コード例 #1
0
ファイル: Program.cs プロジェクト: h09shais/CommandPattern
        static void Main(string[] arguments)
        {
            var argument = arguments.Length > 0 ? arguments[0].ToUpper() : null;

            ISwitchable lamp = new Light();

            ICommand switchClose = new SwitchOnCommand(lamp);
            ICommand switchOpen  = new SwitchOffCommand(lamp);

            var @switch = new Switch(switchClose, switchOpen);

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

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

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

            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            SimpleRemoteControl remote = new SimpleRemoteControl();

            Light            light          = new Light();
            SwitchOnCommand  switchOnLight  = new SwitchOnCommand(light);
            SwitchOffCommand switchOffLight = new SwitchOffCommand(light);
            Door             door           = new Door();
            SwitchOnCommand  openDoor       = new SwitchOnCommand(door);
            SwitchOffCommand closeDoor      = new SwitchOffCommand(door);

            remote.SetOnSlot(0, switchOnLight);
            remote.SetOffSlot(0, switchOffLight);
            remote.SetOnSlot(1, openDoor);
            remote.SetOffSlot(1, closeDoor);
            remote.OnButtonWasPressed(0);
            remote.OnButtonWasPressed(1);
            remote.OffButtonWasPressed(0);
            remote.OffButtonWasPressed(1);
            remote.UndoButtonWasPushed();

            Console.ReadKey();
        }