示例#1
0
        static void Main(string[] args)
        {
            ConcreteReceiver receiver = new ConcreteReceiver();

            //Create commands for the receiver
            ConcreteCommand1 c1 = new ConcreteCommand1(receiver);
            ConcreteCommand2 c2 = new ConcreteCommand2(receiver);
            ConcreteCommand3 c3 = new ConcreteCommand3(receiver);

            //Calls commands on the receiver
            Invoker invoker = new Invoker(c1);

            invoker.DoCommand(); // Calls action1 on the receiver

            invoker.Command = c2;
            invoker.DoCommand();// Calls action2 on the receiver

            invoker.Command = c3;
            invoker.DoCommand();// Calls action3 on the receiver
        }
示例#2
0
        static void Main(string[] args)
        {
            ServiceLocator.Register <IReceiver1>(new Receiver1());
            ServiceLocator.Register <IReceiver2>(new Receiver2());



            var receiver1 = ServiceLocator.GetService <IReceiver1>();
            var cmd1      = new ConcreteCommand1(receiver1);

            var invoker1 = new SimpleInvoker(cmd1);

            invoker1.Consume();

            var invoker2 = new ComplexInvoker();

            invoker2.AddCommand(new ConcreteCommand1(receiver1));
            invoker2.AddCommand(new ConcreteCommand2());
            invoker2.Consume();



            Console.ReadKey();
        }