Inheritance: Mediator.IMediator
Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // Step1.定义中介者对象
            ConcreteMediator mediator = new ConcreteMediator();

            // Step2.定义同事对象
            Button   addButton       = new Button();
            List     list            = new List();
            ComboBox cb              = new ComboBox();
            TextBox  userNameTextBox = new TextBox();

            addButton.SetMediator(mediator);
            list.SetMediator(mediator);
            cb.SetMediator(mediator);
            userNameTextBox.SetMediator(mediator);

            mediator.addButton       = addButton;
            mediator.list            = list;
            mediator.cb              = cb;
            mediator.userNameTextBox = userNameTextBox;

            // Step3.点击增加按钮
            addButton.Changed();

            Console.WriteLine("---------------------------------------------");

            // Step4.从列表框选择客户
            list.Changed();
        }
Exemplo n.º 2
0
        public void Send_a_message_to_all_colleagues()
        {
            // Arrange
            var(millerWriter, miller)     = CreateConcreteColleague("Miller");
            var(orazioWriter, orazio)     = CreateConcreteColleague("Orazio");
            var(fletcherWriter, fletcher) = CreateConcreteColleague("Fletcher");

            var mediator       = new ConcreteMediator(miller, orazio, fletcher);
            var expectedOutput = @"[Miller]: Hey everyone!
[Orazio]: What's up Miller?
[Fletcher]: Hey Miller!
";

            // Act
            mediator.Send(new Message(
                              from: miller,
                              content: "Hey everyone!"
                              ));
            mediator.Send(new Message(
                              from: orazio,
                              content: "What's up Miller?"
                              ));
            mediator.Send(new Message(
                              from: fletcher,
                              content: "Hey Miller!"
                              ));

            // Assert
            Assert.Equal(expectedOutput, millerWriter.Output.ToString());
            Assert.Equal(expectedOutput, orazioWriter.Output.ToString());
            Assert.Equal(expectedOutput, fletcherWriter.Output.ToString());
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            ConcreteMediator mediator = new ConcreteMediator();

            ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator);
            ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator);

            mediator.Colleague1 = colleague1;
            mediator.Colleague2 = colleague2;

            colleague1.Send("吃過飯了嗎?");
            colleague2.Send("沒有呢,你打算請客?");

            Console.WriteLine("");

            UnitedNationsSecurityCouncil UNSC = new UnitedNationsSecurityCouncil();

            USA  usa  = new USA(UNSC);
            Iraq iraq = new Iraq(UNSC);

            UNSC.Colleague1 = usa;
            UNSC.Colleague2 = iraq;

            usa.Delcare("不准研製核武器,否則要發動戰爭!");
            iraq.Declare("我們沒有核武器,也不怕侵略");

            Console.ReadLine();
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            IMediator mediator = new ConcreteMediator();

            User1 user1 = new User1(mediator);
            User2 user2 = new User2(mediator);

            user2.Publish($"Mesaj {typeof(User2).Name}'den yayınlandı.");
            Console.Read();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            ConcreteMediator  mediator  = new ConcreteMediator();
            ConcreteColleague colleague = new ConcreteColleague(mediator);

            mediator.Register(colleague);

            mediator.Operation();

            Console.ReadKey();
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            ConcreteMediator  mediator  = new ConcreteMediator();
            ConcreteCollegue1 collegue1 = new ConcreteCollegue1(mediator);
            ConcreteCollegue2 collegue2 = new ConcreteCollegue2(mediator);

            mediator.Collegue1 = collegue1;
            mediator.Collegue2 = collegue2;
            collegue1.Send("Hello from collegue 1");
            collegue2.Send("Hello from collegue 2");
        }
Exemplo n.º 7
0
        static void Main()
        {
            var mediator = new ConcreteMediator();
            var colleague1 = new ConcreteColleague1(mediator);
            var colleague2 = new ConcreteColleague2(mediator);

            mediator.Colleague1 = colleague1;
            mediator.Colleague2 = colleague2;
            colleague1.Send("Message A");
            colleague2.Send("Message B");
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            ConcreteMediator   m  = new ConcreteMediator();
            ConcreteColleague1 c1 = new ConcreteColleague1(m);
            ConcreteColleague2 c2 = new ConcreteColleague2(m);

            m.Colleague1 = c1;
            m.Colleague2 = c2;
            c1.Send("How are you?");
            c2.Send("Fine, thanks");
            Console.ReadKey();
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            ConcreteMediator   m   = new ConcreteMediator();
            ConcreteColleague1 cc1 = new ConcreteColleague1(m);
            ConcreteColleague2 cc2 = new ConcreteColleague2(m);

            m.CC1 = cc1;
            m.CC2 = cc2;

            cc1.Send("hello, how r u?");
            cc2.send("Fine, Thanks. Would u like some coffee sometimes");
        }
Exemplo n.º 10
0
        static void Pattern()
        {
            var mediator   = new ConcreteMediator();
            var colleague1 = new ConcreteColleague1(mediator);
            var colleague2 = new ConcreteColleague2(mediator);

            mediator.Colleague1 = colleague1;
            mediator.Colleague2 = colleague2;

            colleague1.Send("message 1");
            colleague2.Send("message 2");
        }
Exemplo n.º 11
0
        private static void Main()
        {
            var mediator = new ConcreteMediator();

            mediator.FirstColleague  = new ConcreteColleague1(mediator);
            mediator.SecondColleague = new ConcreteColleague2(mediator);

            mediator.FirstColleague.Send("How Are you?");
            mediator.SecondColleague.Send("Fine, thanks");

            Console.ReadLine();
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            ConcreteMediator mediator = new ConcreteMediator();
            Suporte          suporte  = new Suporte(mediator);
            Usuario          usuario  = new Usuario(mediator);

            mediator.Suporte = suporte;
            mediator.Usuario = usuario;
            usuario.Send("Meu Windows não está entrando!!!");
            suporte.Send("Formate a máquina...");
            Console.ReadLine();
        }
Exemplo n.º 13
0
        static void Main(string[] args)
        {
            ConcreteMediator   mediator   = new ConcreteMediator();
            ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator);
            ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator);

            mediator.Colleague2 = colleague2;
            mediator.Colleague1 = colleague1;
            colleague1.Send("How are you?");
            colleague2.Send("Fine, thanks!!");

            Console.ReadKey();
        }
Exemplo n.º 14
0
        static void Main()
        {
            var m = new ConcreteMediator();

            var c1 = new ConcreteColleague1(m);
            var c2 = new ConcreteColleague2(m);

            m.Colleague1 = c1;
            m.Colleague2 = c2;

            c1.Send("How are you?");
            c2.Send("Fine, thanks");
        }
Exemplo n.º 15
0
        public static void Main(string[] args)
        {
            ConcreteMediator   m  = new ConcreteMediator();
            ConcreteColleague1 c1 = new ConcreteColleague1(m);
            ConcreteColleague2 c2 = new ConcreteColleague2(m);

            m.C1 = c1;
            m.C2 = c2;

            c1.Send("Hello c2");
            c2.Send("Hello c1");

            Console.Read();
        }
Exemplo n.º 16
0
        static void Main(string[] args)
        {
            ConcreteMediator  m  = new ConcreteMediator();
            ConcreteColleage1 c1 = new ConcreteColleage1(m);//讓2個具體同事類別認識仲介者
            ConcreteColleage2 c2 = new ConcreteColleage2(m);

            m.Colleage1 = c1;
            m.Colleage2 = c2;

            c1.Send("今天吃什麼?");
            c2.Send("不知道");

            Console.ReadKey();
        }
Exemplo n.º 17
0
        private static void Main()
        {
            var component1       = new Component1();
            var component2       = new Component2();
            var concreteMediator = new ConcreteMediator(component1, component2);

            Console.WriteLine("Client triggers operation A.");
            component1.DoA();
            Console.WriteLine();
            Console.WriteLine("Client triggers operation D.");
            component2.DoD();

            concreteMediator.Notify(55, "A");
        }
Exemplo n.º 18
0
        static void Main(string[] args)
        {
            var mediator = new ConcreteMediator();

            var participantA = new ConcreteParticipantA(mediator);
            var participantB = new ConcreteParticipantB(mediator);


            mediator.participantA = participantA;
            mediator.participantB = participantB;

            participantA.Send("Hi to ParticipantB from ParticipantA");
            Console.WriteLine();
            participantB.Send("Hi to ParticipantA from participantB");
        }
Exemplo n.º 19
0
        static void Main(string[] args)
        {
            ConcreteMediator m = new ConcreteMediator();

            ConcreteCollegue1 c1 = new ConcreteCollegue1(m);
            ConcreteCollegue2 c2 = new ConcreteCollegue2(m);

            m.ConcreteCollegue1 = c1;
            m.ConcreteCollegue2 = c2;

            c1.Send("呃呃呃");
            c2.Send("呼呼呼");

            Console.Read();
        }
Exemplo n.º 20
0
        static void Main(string[] args)
        {
            ConcreteMediator   _mediator   = new ConcreteMediator();
            ConcreteColleague1 _concrete01 = new ConcreteColleague1(_mediator);
            ConcreteColleague2 _concrete02 = new ConcreteColleague2(_mediator);

            _mediator.Colleague1 = _concrete01;
            _mediator.Colleague2 = _concrete02;

            _concrete01.Send("Hi,there!");

            _concrete02.Send("Great things will come! We know it!");

            Console.ReadKey();
        }
Exemplo n.º 21
0
        static void Main(string[] args)
        {
            ConcreteMediator mediator = new ConcreteMediator();

            ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator);
            ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator);

            mediator.Colleague1 = colleague1;
            mediator.Colleague2 = colleague2;

            colleague1.Send("Message 1");
            colleague2.Send("Message 2");

            Console.Read();
        }
Exemplo n.º 22
0
        static void Main()
        {
            ConcreteMediator m = new ConcreteMediator();

            ConcreteColleague1 c1 = new ConcreteColleague1(m);
            ConcreteColleague2 c2 = new ConcreteColleague2(m);

            m.Colleague1 = c1;
            m.Colleague2 = c2;

            c1.Send("How are you?");
            c2.Send("Fine, thanks");
     
            Console.ReadKey();
        }
Exemplo n.º 23
0
        private static void Main()
        {
            var mediator = new ConcreteMediator();

            var concreteColleagueA = new ConcreteColleagueA(mediator);
            var concreteColleagueB = new ConcreteColleagueB(mediator);

            mediator.ColleagueA = concreteColleagueA;
            mediator.ColleagueB = concreteColleagueB;

            concreteColleagueA.Send("How are you?");
            concreteColleagueA.Send("Fine, thanks");

            Console.ReadKey();
        }
Exemplo n.º 24
0
        static void Main(string[] args)
        {
            ConcreteMediator mediator = new ConcreteMediator();
            ConcreteObject obj1 = new ConcreteObject(mediator) { Name = "obj1" };
            ConcreteObject obj2 = new ConcreteObject(mediator) { Name = "obj2" };
            ConcreteObject obj3 = new ConcreteObject(mediator) { Name = "obj3" };

            obj1.Send("Hello, World!");
            Console.WriteLine("---------");
            obj2.Send("Hi, my name is vaka-vaka");
            Console.WriteLine("---------");
            obj3.Send("Yo-yo-yo, Albuquerque");

            Console.ReadLine();
        }
Exemplo n.º 25
0
        static void Main()
        {
            var mediator = new ConcreteMediator();
            var farmer   = new Farmer(mediator);
            var cannery  = new Cannery(mediator);
            var shop     = new Shop(mediator);

            mediator.Farmer  = farmer;
            mediator.Cannery = cannery;
            mediator.Shop    = shop;

            farmer.GrowTomato();

            // Delay.
            Console.ReadKey();
        }
Exemplo n.º 26
0
        static void Main(string[] args)
        {
            Participant <string> p1 = new ConcreteParticipant <string>("p1");
            Participant <string> p2 = new ConcreteParticipant <string>("p2");
            Participant <string> p3 = new ConcreteParticipant <string>("p3");

            Mediator <string> m = new ConcreteMediator <string>();

            m.Register(p1);
            m.Register(p2);
            m.Register(p3);

            p1.SendMessage(m, "Message from p1");

            Console.ReadKey();
        }
Exemplo n.º 27
0
        static void Main(string[] args)
        {
            //中介者模式
            ConcreteMediator cm = new ConcreteMediator();

            ConcreteColleague1 colleague1 = new ConcreteColleague1(cm);
            ConcreteColleague2 colleague2 = new ConcreteColleague2(cm);

            cm.Concretecolleague1 = colleague1;
            cm.Concretecolleague2 = colleague2;

            colleague1.Send("Hello");
            colleague2.Send("Hi");

            Console.Read();
        }
Exemplo n.º 28
0
        static void Main()
        {
            var mediator = new ConcreteMediator();
            var waiter   = new Waiter(mediator);
            var cook     = new Cook(mediator);

            mediator.Waiter = waiter;
            mediator.Cook   = cook;

            Console.WriteLine("Официант ожидает заказ:");
            string order = Console.ReadLine();

            waiter.TakeAnOrder(order);

            Console.ReadKey();
        }
Exemplo n.º 29
0
        static void Main()
        {
            ConcreteMediator   m  = new ConcreteMediator();
            ConcreteColleague1 c1 = new ConcreteColleague1(m);
            ConcreteColleague2 c2 = new ConcreteColleague2(m);
            ConcreteColleague3 c3 = new ConcreteColleague3(m);

            m.Colleague1 = c1;
            m.Colleague2 = c2;
            m.Colleague3 = c3;
            m.Send("How are you?", c1);
            m.Send("Fine, thanks", c2);
            m.Send("Hello!", c3);
            // Wait for user
            Console.Read();
        }
Exemplo n.º 30
0
        static public void Main()
        {
            //构建具体类
            ConcreteMediator   concreteMediator   = new ConcreteMediator();
            ConcreteColleagueA concreteColleagueA = new ConcreteColleagueA(concreteMediator);
            ConcreteColleagueB concreteColleagueB = new ConcreteColleagueB(concreteMediator);
            ConcreteColleagueC concreteColleagueC = new ConcreteColleagueC(concreteMediator);

            //各司其职
            concreteColleagueA.DoSelfDuty();
            concreteColleagueB.DoSelfDuty();
            concreteColleagueC.DoSelfDuty();

            //得到最终产品
            FinalProduct finalProduct = concreteMediator.GetFinalProduct();
        }
Exemplo n.º 31
0
        static void Main(string[] args)
        {
            var mediator    = new ConcreteMediator();
            var componentA1 = new ComponentA("a1");
            var componentA2 = new ComponentA("a2");
            var componentB1 = new ComponentB("b1");
            var componentB2 = new ComponentB("b2");

            mediator.AddComponent(componentA1);
            mediator.AddComponent(componentA2);
            mediator.AddComponent(componentB1);
            mediator.AddComponent(componentB2);

            componentA2.Notify("hello world", "b2");
            componentB1.Broadcast("hello world all");
        }
Exemplo n.º 32
0
        private static void Main()
        {
            ConcreteMediator m = new ConcreteMediator();

            ConcreteColleague1 c1 = new ConcreteColleague1(m);
            ConcreteColleague2 c2 = new ConcreteColleague2(m);

            m.Colleague1 = c1;
            m.Colleague2 = c2;

            c1.Send("How are you?");
            c2.Send("Fine, thanks");

            // Wait for user
            Console.ReadKey();
        }
Exemplo n.º 33
0
        static void Main(string[] args)
        {
            Console.WriteLine("MEDIATOR DESIGN PATTERN\n");

            Console.WriteLine("------------");

            ConcreteMediator mediator = new ConcreteMediator();
            UserA            userA    = new UserA(mediator);
            UserB            userB    = new UserB(mediator);

            mediator.UserA = userA; //set user A reference to mediator
            mediator.UserB = userB; //set user B reference to mediator

            userA.Send("Hi");
            Console.WriteLine("----------");
            userB.Send("Hello");
        }
Exemplo n.º 34
0
        static void Main(string[] args)
        {
            Console.WriteLine("中介者模式:用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显示地相互引用,从而使其耦合松散,从而可以独立地改变他们之间的交互。中介者模式又称为调停者模式,它是一种对象行为型模式。");

            ConcreteMediator mediator = new ConcreteMediator();
            Button bt = new Button();
            List lt = new List();
            TextBox tb = new TextBox();

            bt.setMediator(mediator);
            lt.setMediator(mediator);
            tb.setMediator(mediator);

            mediator.button = bt;
            mediator.list = lt;
            mediator.textBox = tb;

            bt.changed();
            lt.changed();

            Console.ReadKey();
        }
Exemplo n.º 35
0
        static void Main()
        {

            ConcreteMediator m = new ConcreteMediator();

            ConcreteColleague1 c1 = new ConcreteColleague1(m);

            ConcreteColleague2 c2 = new ConcreteColleague2(m);



            m.Colleague1 = c1;

            m.Colleague2 = c2;



            c1.Send("Как дела?");

            c2.Send("Хорошо спасибо");

            Console.ReadLine();

        }
Exemplo n.º 36
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();
        }
Exemplo n.º 37
0
        static void Main()
        {
            var m = new ConcreteMediator();

            var c1 = new ConcreteColleague1(m);
            var c2 = new ConcreteColleague2(m);

            m.Colleague1 = c1;
            m.Colleague2 = c2;

            c1.Send("How are you?");
            c2.Send("Fine, thanks");
        }