Ejemplo n.º 1
0
        static void Main()
        {
            // Создание издателя.
            ConcreteSubject subject = new ConcreteSubject();

            // Создание подписчиков.
            ConcreteObserver observer1 = new ConcreteObserver("1", subject);
            ConcreteObserver observer2 = new ConcreteObserver("2", subject);
            ConcreteObserver observer3 = new ConcreteObserver("3", subject);
            ConcreteObserver observer4 = new ConcreteObserver("4", subject);

            // Подписание подписчиков на издателя с получением объекта для отписки.
            IDisposable unsubscriber1 = subject.Subscribe(observer1);
            IDisposable unsubscriber2 = subject.Subscribe(observer2);
            IDisposable unsubscriber3 = subject.Subscribe(observer3);
            IDisposable unsubscriber4 = subject.Subscribe(observer4);

            using (subject)
            {
                // Попытка предоставить обзерверам некорректное состояние.
                subject.State = null;
                subject.Notify();

                Console.WriteLine(new string('-', 70) + "1");

                // Отписка первого подписчика через ConcreteSubject.Unsubscriber.Dispose()
                using (unsubscriber1)
                {
                    // Попытка предоставить обзерверам корректное состояние.
                    subject.State = "State 1 ...";
                    subject.Notify();
                }

                Console.WriteLine(new string('-', 70) + "2");

                // State 2 - получат только три подписчика которые остались подписанными.
                subject.State = "State 2 ...";
                subject.Notify();

                Console.WriteLine(new string('-', 70) + "3");

                // Отписка второго подписчика через ConcreteObserver.OnCompleted()
                observer2.OnCompleted();

                // State 3 - получат только 2 подписчика которые остались подписанными.
                subject.State = "State 3 ...";
                subject.Notify();
            } // observers.Clear()

            Console.WriteLine(new string('-', 70) + "4");

            // Попытка отписать уже отписанного подписчика, обрабатывается в
            // ConcreteSubject.Unsubscriber.Dispose()
            observer4.OnCompleted();

            // Delay.
            Console.ReadKey();
        }
Ejemplo n.º 2
0
		static void Main(string[] args) {
			var subject = new ConcreteSubject();

			subject.Attach(new ConcreteObserver(subject));
			subject.Attach(new ConcreteObserver(subject));

			subject.State = "State1";
			subject.Notify();

			subject.State = "State2";
			subject.Notify();

			Console.ReadKey();
		}
Ejemplo n.º 3
0
        private static void Main(string[] args)
        {
            var subject = new ConcreteSubject();

            subject.Attach(new ConcreteObserver(subject));
            subject.Attach(new ConcreteObserver(subject));

            subject.State = "State1";
            subject.Notify();

            subject.State = "State2";
            subject.Notify();

            Console.ReadKey();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            #region 基本用法(Basic.cs)
            ConcreteSubject s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "X"));
            s.Attach(new ConcreteObserver(s, "Y"));
            s.Attach(new ConcreteObserver(s, "Z"));

            s.SubjectState = "ABC";
            s.Notify();
            #endregion

            #region 具体实例(Example.cs)
            //相比于基本用法,这个实例:
            //①进一步将通知者和观察者分离,运用依赖倒转原则,在客户端中将观察者动作委托给通知者,进一步降低了耦合
            //②观察者实例无需继承观察者接口

            Boss huhanshan = new Boss();

            StockObserver tongshi1 = new StockObserver("魏关姹", huhanshan);
            NBAOberver    tongshi2 = new NBAOberver("易管查", huhanshan);

            huhanshan.Update += new EventHandler(tongshi1.CloseStockMarket);
            huhanshan.Update += new EventHandler(tongshi2.CloseNBADirectSeeding);

            huhanshan.SubjectState = "我胡汉三回来了";
            huhanshan.Notify();

            Console.Read();
            #endregion
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            #region 基本用法(Basic.cs)
            ConcreteSubject s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "X"));
            s.Attach(new ConcreteObserver(s, "Y"));
            s.Attach(new ConcreteObserver(s, "Z"));

            s.SubjectState = "ABC";
            s.Notify();
            #endregion

            #region 具体实例(Example.cs)
            //相比于基本用法,这个实例:
            //①进一步将通知者和观察者分离,运用依赖倒转原则,在客户端中将观察者动作委托给通知者,进一步降低了耦合
            //②观察者实例无需继承观察者接口

            Boss huhanshan = new Boss();

            StockObserver tongshi1 = new StockObserver("魏关姹", huhanshan);
            NBAOberver tongshi2 = new NBAOberver("易管查", huhanshan);

            huhanshan.Update += new EventHandler(tongshi1.CloseStockMarket);
            huhanshan.Update += new EventHandler(tongshi2.CloseNBADirectSeeding);

            huhanshan.SubjectState = "我胡汉三回来了";
            huhanshan.Notify();

            Console.Read();
            #endregion
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            /*
             * More info: https://en.wikipedia.org/wiki/Observer_pattern
             * What problems can the Observer design pattern solve?
             *
             * A one-to-many dependency between objects should be defined without making the objects tightly coupled.
             * It should be ensured that when one object changes state an open-ended number of dependent objects are updated automatically.
             * It should be possible that one object can notify an open-ended number of other objects.
             *
             * Defining a one-to-many dependency between objects by defining one object (subject)
             * that updates the state of dependent objects directly is inflexible because it commits (tightly couples) the subject to particular dependent objects.
             *
             * Tightly coupled objects are hard to implement, change, test,
             * and reuse because they refer to and know about (how to update) many different objects with different interfaces.
             */


            #region Observer pattern

            // Configure Observer pattern

            ConcreteSubject subject = new ConcreteSubject();

            subject.Attach(new ConcreteObserver(subject, "X"));

            subject.Attach(new ConcreteObserver(subject, "Y"));

            subject.Attach(new ConcreteObserver(subject, "Z"));


            // Change subject and notify observers

            subject.SubjectState = "ABC";

            subject.Notify();

            #endregion

            #region .Net implementation of observer pattern

            var publisherWeatherStation = new WeatherReport();

            var subscriberGermanyWeatherStation     = new GermanyWeatherStation();
            var subscriberIranWeatherStation        = new IranWeatherStation();
            var subscriberUnitedStateWeatherStation = new UnitedStateWeatherStation();

            publisherWeatherStation.WeatherChanged += subscriberGermanyWeatherStation.GermanyWeatherChanged;
            publisherWeatherStation.WeatherChanged += subscriberIranWeatherStation.IranWeatherChanged;
            publisherWeatherStation.WeatherChanged += subscriberUnitedStateWeatherStation.UnitedStateWeatherChanged;

            publisherWeatherStation.ReportWeather(new WeatherEventArgs(temperature: 30));

            #endregion

            Console.ReadKey();
        }
Ejemplo n.º 7
0
        static void Main()
        {
            ConcreteSubject subject = new ConcreteSubject();

            subject.Attach(new ConcreteObserver(subject));
            subject.Attach(new ConcreteObserver(subject));
            subject.State = "Some State ...";
            subject.Notify();
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            ConcreteSubject cs = new ConcreteSubject();

            cs.Attach(new ConcreteObserver(1));
            cs.Attach(new ConcreteObserver(2));
            cs.Notify();
            Console.Read();
        }
Ejemplo n.º 9
0
        static void Main()
        {
            //For both models
            var subject = new ConcreteSubject();

            subject.Attach(new ConcreteObserver(subject));
            subject.Attach(new ConcreteObserver(subject));
            subject.State = "State";
            subject.Notify();
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            Subject subject = new ConcreteSubject();

            subject.Attach(new ConcreteObserver(subject, "X"));
            subject.Attach(new ConcreteObserver(subject, "Y"));
            subject.SubjectState = "ABC";
            subject.Notify();
            Console.ReadLine();
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            ConcreteSubject s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "x"));
            s.Attach(new ConcreteObserver(s, "Y"));

            s.SubjectState = "hello ,how r u";
            s.Notify();
        }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            ConcreteSubject cs = new ConcreteSubject();

            cs.Attach(new ConcreteObserver(cs, "James"));
            cs.Attach(new ConcreteObserver(cs, "Jane"));

            cs.SubjectState = "OK";
            cs.Notify();
            Console.Read();
        }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            ConcreteSubject s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "X"));
            s.Attach(new ConcreteObserver(s, "Y"));
            s.Attach(new ConcreteObserver(s, "Z"));
            s.SubjectState = "ABC";
            s.Notify();
            Console.ReadKey();
        }
Ejemplo n.º 14
0
    {   /// <summary>
        /// 利用抽象类和接口实现观察者模式,利用事件实现见事件代码
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            ConcreteSubject subject = new ConcreteSubject();

            subject.Attach(new ConcerteObserver(subject, "jia"));
            subject.Attach(new ConcerteObserver(subject, "binf"));
            subject.Attach(new ConcerteObserver(subject, "yi"));
            subject.SubjectStste = "12121";
            subject.Notify();
            Console.Read();
        }
        public static void Run()
        {
            Console.WriteLine("Observer Structural Practice");
            ConcreteSubject s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "X"));
            s.Attach(new ConcreteObserver(s, "Y"));
            s.Attach(new ConcreteObserver(s, "Z"));

            s.SubjectState = "ABC";
            s.Notify();
        }
Ejemplo n.º 16
0
        static void Main(string[] args)
        {
            ConcreteSubject subject = new ConcreteSubject();

            subject.State = "Мурзилка";

            ConcreteObserver observer = new ConcreteObserver(subject, "Вася");

            subject.Attach(observer);
            subject.Notify();

            System.Console.WriteLine($"{observer.Name} подписан на журнал {observer.ObserverState}");
        }
Ejemplo n.º 17
0
        static void Main(string[] args)
        {
            ConcreteSubject concreteSubject = new ConcreteSubject();

            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "Observer A"));
            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "Observer B"));
            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "Observer C"));

            concreteSubject.State = "Loading...";
            concreteSubject.Notify();

            Console.Read();
        }
Ejemplo n.º 18
0
        private static void Main()
        {
            var concreteSubject = new ConcreteSubject();

            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "X"));
            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "Y"));
            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "Z"));

            concreteSubject.SubjectState = "ABC";
            concreteSubject.Notify();

            Console.ReadKey();
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Entry point into console application.
        /// </summary>
        static void Main()
        {
            // Configure Observer pattern
            ConcreteSubject s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "X"));
            s.Attach(new ConcreteObserver(s, "Y"));
            s.Attach(new ConcreteObserver(s, "Z"));

            // Change subject and notify observers
            s.SubjectState = "ABC";
            s.Notify();
        }
Ejemplo n.º 20
0
        static void Main(string[] args)
        {
            ConcreteSubject concreteSubject = new ConcreteSubject();

            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "Observer A"));
            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "Observer B"));
            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "Observer C"));

            concreteSubject.State = "Loading...";
            concreteSubject.Notify();

            Console.Read();
        }
Ejemplo n.º 21
0
        static void Main()
        {
            ConcreteSubject s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "X"));
            s.Attach(new ConcreteObserver(s, "Y"));
            s.Attach(new ConcreteObserver(s, "Z"));


            s.SubjectState = "ABC";
            s.Notify();

            Console.ReadKey();
        }
Ejemplo n.º 22
0
        static void Main(string[] args)
        {
            // Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
            Console.WriteLine("Hello World!");

            ConcreteSubject s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "X"));
            s.Attach(new ConcreteObserver(s, "Y"));
            s.Attach(new ConcreteObserver(s, "Z"));

            s.SubjectState = "ABC";
            s.Notify();
        }
Ejemplo n.º 23
0
        static void Main(string[] args)
        {
            ConcreteSubject concreteSubject = new ConcreteSubject();

            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "x"));
            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "y"));
            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "z"));

            concreteSubject.SubjectState = "hello";

            concreteSubject.Notify();

            Console.ReadLine();
        }
Ejemplo n.º 24
0
        static void Main()
        {
            ConcreteSubject sub = new ConcreteSubject();

            sub.Attach(new ConcreteObserver(sub, "X"));
            sub.Attach(new ConcreteObserver(sub, "Y"));
            sub.Attach(new ConcreteObserver(sub, "Z"));

            //Change subject and notify observers
            sub.SubjectState = "ABC";
            sub.Notify();

            Console.ReadKey();
        }
Ejemplo n.º 25
0
        static void Main(string[] args)
        {
            ConcreteSubject subject = new ConcreteSubject();

            subject.Attach(new ConcreteObserver(subject, "Object1"));
            subject.Attach(new ConcreteObserver(subject, "Object2"));
            subject.Attach(new ConcreteObserver(subject, "Object3"));

            // Change subject and notify observers
            subject.SubjectState = "NEW STATE";
            subject.Notify();

            // Wait for user
            Console.ReadKey();
        }
Ejemplo n.º 26
0
        static void Main(string[] args)
        {
            var s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "X"));
            s.Attach(new ConcreteObserver(s, "Y"));
            s.Attach(new ConcreteObserver(s, "Z"));

            // Change subject and notify observers
            s.SubjectState = "ABC";
            s.Notify();

            // Wait for user
            Console.ReadKey();
        }
Ejemplo n.º 27
0
        static void Main()
        {
            // Издатель.
            Subject subject = new ConcreteSubject();

            // Подписчик, с сообщенным лямбда выражением.
            Observer observer = new Observer((observerState) => Console.WriteLine(observerState + " 1"));

            // Подписка на уведомление о событии.
            subject.Event += observer;
            subject.Event += (observerState) => Console.WriteLine(observerState + " 2");

            subject.State = "State ...";
            subject.Notify();

            Console.WriteLine(new string('-', 11));

            // Отписка от уведомлений.
            subject.Event -= observer;
            subject.Notify();

            // Delay.
            Console.ReadKey();
        }
Ejemplo n.º 28
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //先创建通知者
            ConcreteSubject subject = new ConcreteSubject();
            //在创建观察者
            ConcreteObserver observer1 = new ConcreteObserver(subject, "学生小明");

            //通知者添加观察者
            subject.Attach(observer1);
            //改状态
            //subject.SubjectState = "学习";
            //通知
            subject.Notify();
            Console.ReadKey();
        }
Ejemplo n.º 29
0
        static void Main(string[] args)
        {
            Console.WriteLine("传统观察者模式-----");

            ConcreteSubject s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "李四"));
            s.Attach(new ConcreteObserver(s, "张三"));
            s.Attach(new ConcreteObserver(s, "王二"));

            s.SubjectState = "工作ing";
            s.Notify();


            Console.Read();
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Entry point into console application.
        /// </summary>
        static void Main()
        {
            // Configure Observer pattern
            ConcreteSubject s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "X"));
            s.Attach(new ConcreteObserver(s, "Y"));
            s.Attach(new ConcreteObserver(s, "Z"));

            // Change subject and notify observers
            s.SubjectState = "ABC";
            s.Notify();

            // Wait for user
            Console.ReadKey();
        }
Ejemplo n.º 31
0
        private static void Main()
        {
            var concreteSubject = new ConcreteSubject();

            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "ConcreteObserverA"));

            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "ConcreteObserverB"));

            concreteSubject.Attach(new ConcreteObserver(concreteSubject, "ConcreteObserverC"));

            concreteSubject.SubjectState = "State";

            concreteSubject.Notify();

            Console.ReadKey();
        }
Ejemplo n.º 32
0
 private void BtNotify_Click(object sender, RoutedEventArgs e)
 {
     if (Cb.SelectedIndex == 0)
     {
         subject.brush = Brushes.Red;
     }
     if (Cb.SelectedIndex == 1)
     {
         subject.brush = Brushes.Black;
     }
     if (Cb.SelectedIndex == 2)
     {
         subject.brush = Brushes.Blue;
     }
     subject.Notify();
 }
        /// <summary>
        /// The test third.
        /// </summary>
        private static void TestThird()
        {
            // Configure Observer pattern
            ConcreteSubject subject = new ConcreteSubject();

            subject.Attach(new ConcreteObserver(subject, "X"));
            subject.Attach(new ConcreteObserver(subject, "Y"));
            subject.Attach(new ConcreteObserver(subject, "Z"));

            // Change subject and notify observers
            subject.SubjectState = "ABC";
            subject.Notify();

            // Wait for user
            Console.ReadKey();
        }
Ejemplo n.º 34
0
        static void Main(string[] args)
        {
            //创建主题对象
            ConcreteSubject Bucciarati = new ConcreteSubject();

            //创建订阅者
            ConcreteObserverA Prosciutto = new ConcreteObserverA("普罗修特");
            ConcreteObserverB Pesci      = new ConcreteObserverB("贝西");

            //订阅者订阅主题对象(委托的多播)
            Bucciarati.Update += new EventHandler(Prosciutto.Attack);
            Bucciarati.Update += new EventHandler(Pesci.Shout);

            //主题对象通知事件
            Bucciarati.Notify();

            Console.Read();
        }
Ejemplo n.º 35
0
        public static void Run()
        {
            Console.WriteLine("This structural code demonstrates the Observer pattern in which registered objects are notified of and updated with a state change.\n");

            ConcreteSubject s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "X"));
            s.Attach(new ConcreteObserver(s, "Y"));
            s.Attach(new ConcreteObserver(s, "Z"));

            s.SubjectState = "ABC";
            s.Notify();

            /*
             * Observer X's new state is ABC
             * Observer Y's new state is ABC
             * Observer Z's new state is ABC
             */
        }
Ejemplo n.º 36
0
        static void Main()
        {
            // First example
            ConcreteSubject s = new ConcreteSubject();
            s.Attach(new ConcreteObserver(s, "x"));
            s.Attach(new ConcreteObserver(s, "y"));
            s.Attach(new ConcreteObserver(s, "z"));
            s.SubjectState = "abc";
            s.Notify();

            IBM ibm = new IBM("IBM", 120.00);
            ibm.Attach(new Investor("Sorros"));
            ibm.Attach(new Investor("Berkshire"));

            ibm.Price = 120.10;
            ibm.Price = 121.00;
            ibm.Price = 120.50;
            ibm.Price = 120.75;
            ibm.Notify();

            // Second example
            BaggageHandler provider = new BaggageHandler();
            ArrivalsMonitor observer1 = new ArrivalsMonitor("BaggageClaimMonitor1");
            ArrivalsMonitor observer2 = new ArrivalsMonitor("SecurityExit");

            provider.BaggageStatus(712, "Detroit", 3);
            observer1.Subscribe(provider);
            provider.BaggageStatus(712, "Kalamazoo", 3);
            provider.BaggageStatus(400, "New York-Kennedy", 1);
            provider.BaggageStatus(712, "Detroit", 3);
            observer2.Subscribe(provider);
            provider.BaggageStatus(511, "San Francisco", 2);
            provider.BaggageStatus(712);
            observer2.Unsubscribe();
            provider.BaggageStatus(400);
            provider.LastBaggageClaimed();

            // Third example
            var weatherData = new WeatherData();
            var forecastDisplay = new ForecastDisplay(weatherData);
            var heatIndexDisplay = new HeatIndexDisplay(weatherData);
            weatherData.setMeasurements(80, 65, 30.4f);
        }
Ejemplo n.º 37
0
        static void Main(string[] args)
        {
            #region 工厂方法
            double total = 0.0d;
            CashContext cc = new CashContext(new CashNormal());
            total = cc.GetResult(100.04);
            cc = new CashContext(new CashRebate("0.8"));
            total = cc.GetResult(100.04);
            Console.WriteLine(total);
            #endregion

            #region 装饰器方法
            Decorator.Component person = new Decorator.Component("xiaocai");

            Tshirt tshirt = new Tshirt();
            BigTrouser bt = new BigTrouser();

            bt.Decorator(person);
            tshirt.Decorator(bt);
            tshirt.show();

            Console.WriteLine("*****************************");
            #endregion

            #region 代理方法
            SchoolGirl sg = new SchoolGirl();
            sg.Name = "李娇骄";
            Proxy.Proxy daili = new Proxy.Proxy(sg);
            daili.GiveDolls();
            daili.GiveFlowers();
            #endregion

            #region 原型模式
            ConcretePrototype1 p1 = new ConcretePrototype1("123");
            ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
            Console.WriteLine("Cloned :"+c1.Id);

            Resume a = new Resume("Andy");
            a.setInfo("Man", "24");
            a.setWorkExperience("1998-2005","IBM ");
            Resume b = (Resume)a.Clone();
            b.setWorkExperience("2002-2005", "Dell");
            a.display();
            b.display();
            #endregion

            #region 模板模式
            Console.WriteLine("Student A testPaper:");
            TestPaperA testA = new TestPaperA();
            testA.Test1();
            testA.Test2();
            Console.WriteLine("Student B testPaper:");
            TestPaperB testB = new TestPaperB();
            testB.Test1();
            testB.Test2();
            #endregion

            #region 抽象工厂方法
            User user = new User();

            IFactory factory = new SqlServerFactory();
            IUser iu = factory.CreateUser();
            //IUser riu = (IUser)Assembly.Load("AbstractFactory").CreateInstance("SqlserverUser");
            //反射
            //Assembly.Load("程序集名称").CreateInstance("程序集名称.类名称");
            iu.Insert(user);
            iu.GetUser(1);
            #endregion

            #region Facade 外观模式
            Fund jijin = new Fund();
            jijin.BuyFund();
            jijin.sellFund();
            #endregion

            #region 建造者模式
            Director director = new Director();
            abstractBuilder builder1 = new Builder1();
            abstractBuilder builder2 = new BuilderB();
            director.Construct(builder1);
            Builder.Builder b1 = builder1.getBuilder();
            b1.show();

            director.Construct(builder2);
            Builder.Builder b2 = builder2.getBuilder();
            b2.show();
            #endregion
            #region 观察者模式
            Observer.ConcreteSubject s = new Observer.ConcreteSubject();
            s.Attach(new Observer.ConcreteObserver(s, "x"));
            s.Attach(new Observer.ConcreteObserver(s, "y"));
            s.SubjectState = "ABC";
            s.Notify();
            ///下面是使用委托
            ///委托就是一种引用方法的类型。一旦为委托分配了方法,委托将于该方法具有完全相同的行为。
            ///委托方法的使用可以像其他的方法一样具有参数和返回值。委托可以看作是对函数的抽象,是函数的”类“,委托的实例将代表一个具体的函数
            ///一个委托可以搭载多个方法,所有方法被依次唤起,委托搭载的方法不需要属于同一个类,只需要具有相同的原型和形式,也就是拥有相同的参数列表和返回类型。
            ///在使用带参数的委托时,只需要在声明事件的地方将参数传递给事件。在绑定时将调用的方法绑定给事件。
            Bosscs boss = new Bosscs();
            StockObserver tongshi1 = new StockObserver("tongshi1",boss);
            NBAObserver tongshiNBA = new NBAObserver("tongshiNBA", boss);
            boss.Update += new EventHandler1(tongshi1.CloseStockMarket);
            boss.Update += new EventHandler1(tongshiNBA.CloseStockMarket);
            boss.update2 += new EventHandler2(tongshiNBA.print);
            boss.SubjectState = " I am back ";
            boss.Notify();
            #endregion

            #region 状态模式
            State.Context c = new State.Context(new CreateStateA());
            c.Request();
            c.Request();
            c.Request();
            c.Request();

            #endregion

            #region 备忘录模式
            Originator o = new Originator();
            o.State = "On";
            o.Show();
            Caretaker care = new Caretaker();
            care.Memento = o.CreateMemento();
            o.State = "Off";
            o.Show();

            o.SetMemento(care.Memento);
            o.Show();

            GameRole gameRole = new GameRole();
            gameRole.GetInitState();
            gameRole.StateDisplay();

            RoleStateManager stateManager = new RoleStateManager();
            stateManager.Memento = gameRole.SaveState();

            gameRole.Fight();
            gameRole.StateDisplay();

            gameRole.RecoveryState(stateManager.Memento);
            gameRole.StateDisplay();
            #endregion

            #region 组合模式
            Composite.Composite root = new Composite.Component("root");
            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));

            Composite.Composite comp = new Composite.Component("comp X");
            comp.Add(new Leaf("Leaf XA"));
            comp.Add(new Leaf("Leaf XB"));
            root.Add(comp);

            Composite.Composite comp2 = new Composite.Component("Comp X2");
            comp2.Add(new Leaf("Leaf X2A"));
            comp2.Add(new Leaf("Leaf X2B"));
            comp.Add(comp2);

            root.Add(new Leaf("Leaf C"));
            Leaf leaf = new Leaf("Leaf D");

            root.Add(leaf);
            root.Display(1);
            root.Remove(leaf);
            root.Display(1);
            #endregion

            #region 迭代器模式
            ConCreteAggregate aggregate = new ConCreteAggregate();
            aggregate[0] = "大鸟";
            aggregate[1] = "小菜";
            aggregate[2]="行李";
            aggregate[3] = "老外";
            aggregate[4] = "小偷";
            Iterator.Iterator myIterator = new ConCreteIterator(aggregate);
            object item = myIterator.First();
            while (!myIterator.IsDone())
            {
                Console.WriteLine(myIterator.CurrentItem() + "请买车票");
                myIterator.Next();
            }
            #endregion

            #region 单例模式
             //所有类都有构造方法,不编码则默认生成空的构造方法,若有显示定义的构造方法,默认的构造方法就会失效。只要将构造方法改写为私有的,外部的程序就不能通过new 来初始化它。
            //通过一个共有的方法来返回类的实例。
            Singleton.Singleton s1 = Singleton.Singleton.GetInstance();
            Singleton.Singleton s2 = Singleton.Singleton.GetInstance();

            if (s1 == s2)
            {
                Console.WriteLine("两个对象是相同的实例。");
            }

            #endregion

            #region 命令模式
            Receiver r = new Receiver();
            Command.Command command = new Command.ConcreteCommand(r);
            Invoker invoker = new Invoker();
            invoker.SetCommand(command);
            invoker.ExecuteCommand();
            #endregion

            #region 职责链模式
            Handler h1 = new ConcreteHandler1();
            Handler h2 = new ConcreteHandler2();
            h1.SetSuccessor(h2);
            int[] requests = { 2, 3, 4, 5, 6, 12, 34, 11, 15 };
            foreach (int request in requests)
            {
                h1.HandlerRequest(request);
            }
            #endregion

            #region 中介者模式
            ConcreteMediator mediator = new ConcreteMediator();
            ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator);
            ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator);
            mediator.Colleague1 = colleague1;
            mediator.Colleague2 = colleague2;
            colleague1.Send("吃饭了吗?");
            colleague2.Send("还没有呢");
            #endregion

            #region 享元模式
            int extri = 22;
            FlyweightFactory f = new FlyweightFactory();
            Flyweight.Flyweight fx = f.GetFlyweight("X");
            fx.Operation(--extri);

            Flyweight.Flyweight fy = f.GetFlyweight("Y");
            fy.Operation(--extri);

            Flyweight.Flyweight fz = f.GetFlyweight("Z");
            fz.Operation(--extri);

            #endregion

            #region 解释器模式
            <<<<<<< HEAD
            Interpreter.Context context = new Interpreter.Context();
            IList<Interpreter.AbstractExpression> list = new List<Interpreter.AbstractExpression>();
            list.Add(new Interpreter.TerminalExpression());
            list.Add(new Interpreter.NormalExpression());
            foreach (Interpreter.AbstractExpression exp in list)
                exp.Interpret(context);
            =======
            Interpreter.Context context1 = new Interpreter.Context();
            IList<AbstractExpression> list = new List<AbstractExpression>();
            list.Add(new TerminalExpression());
            list.Add(new NonTerminalExpression());
            foreach (AbstractExpression exp in list)
            {
                exp.Interpreter(context1);
            }
            #endregion

            #region 访问者模式
            ObjectStructure os = new ObjectStructure();
            os.Add(new Man());
            os.Add(new Woman());
            Success v1 = new Success();
            os.Display(v1);
            Failing f1 = new Failing();
            os.Display(f1);

            Amativeness a1 = new Amativeness();
            os.Display(a1);
            >>>>>>> 77e342ef6e96917a8dc01e72e41626dcffd4ba13
            #endregion
            Console.Read();
        }