示例#1
0
        public static void Main(string[] arguments)
        {
            string argument = arguments.Length > 0 ? arguments[0].ToUpper() : null;

            ISwitchable lamp = new Light();

            //Pass reference to the lamp instance to each command
            ICommand switchClose = new CloseSwitchCommand(lamp);
            ICommand switchOpen  = new OpenSwitchCommand(lamp);

            //Pass reference to instances of the Command objects to the switch
            Switch @switch = new Switch(switchClose, switchOpen);

            if (argument == "ON")
            {
                // Switch (the Invoker) will invoke Execute() on the command object.
                @switch.Close();
            }
            else if (argument == "OFF")
            {
                //Switch (the Invoker) will invoke the Execute() on the command object.
                @switch.Open();
            }
            else
            {
                Console.WriteLine("Argument \"ON\" or \"OFF\" is required.");
            }
        }
示例#2
0
        /*
         * Intent
         * encapsulate a request in an object
         * allows the parameterization of clients with different requests
         * allows saving the requests in a queue
         */
        static void Main(string[] args)
        {
            var         argument = args.Length > 0 ? args[0].ToUpper() : null;
            ISwitchable lamp     = new Light();

            ICommand switchClose = new CloseSwitchCommand(lamp);
            ICommand switchOpen  = new OpenSwitchCommand(lamp);
            var      @switch     = new Switch(switchClose, switchOpen);

            switch (argument)
            {
            case "ON":
                @switch.Close();
                break;

            case "OFF":
                @switch.Open();
                break;

            default:
                Console.WriteLine("Argument \"ON\" or \"OFF\" is required.");
                break;
            }
        }