コード例 #1
0
        public void TestTVOff()
        {
            Device       device  = new Television();
            Command      command = new Off(device);
            DeviceButton button  = new DeviceButton(command);

            button.press();
            button.press();
            Assert.AreEqual(device.status, Status.off);
        }
コード例 #2
0
        public void TestRadioOn()
        {
            Device       device  = new Radio();
            Command      command = new On(device);
            DeviceButton button  = new DeviceButton(command);

            button.press();
            Assert.AreEqual(device.status, Status.on);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: rgonzalezv/Design-Patterns
        static void Main(string[] args)
        {
            IElectronicDevice newDevice = TVRemote.GetDevice();

            ICommand     onCommand = new TurnTVOn(newDevice);
            DeviceButton onPressed = new DeviceButton(onCommand);

            onPressed.press();

            ICommand offCommand = new TurnTVOff(newDevice);

            onPressed = new DeviceButton(offCommand);
            onPressed.press();

            ICommand volUpCommand = new TurnTVUp(newDevice);

            onPressed = new DeviceButton(volUpCommand);
            onPressed.press();
            onPressed.press();
            onPressed.press();

            ICommand volDownCommand = new TurnTVDown(newDevice);

            onPressed = new DeviceButton(volDownCommand);
            onPressed.press();

            // ---------------------------------- //

            Television theTV    = new Television();
            Radio      theRadio = new Radio();

            List <IElectronicDevice> allDevices = new List <IElectronicDevice>();

            allDevices.Add(theTV);
            allDevices.Add(theRadio);

            TurnItAllOff turnOffDevices = new TurnItAllOff(allDevices);
            DeviceButton turnThemOff    = new DeviceButton(turnOffDevices);

            turnThemOff.press();
        }
コード例 #4
0
        public void TestVolumeDownRadio()
        {
            int expected = 4;

            Device       device  = new Radio();
            Command      command = new VolumeDown(device);
            DeviceButton button  = new DeviceButton(command);

            for (int i = 0; i < 6; i++)
            {
                button.press();
            }

            Assert.AreEqual(expected, device.volume);
        }
コード例 #5
0
        public void TestVolumeDownTV()
        {
            int expected = 5;

            Device       device  = new Television();
            Command      command = new VolumeDown(device);
            DeviceButton button  = new DeviceButton(command);

            for (int i = 0; i < 5; i++)
            {
                button.press();
            }

            Assert.AreEqual(expected, device.volume);
        }
コード例 #6
0
        public void TestUndo()
        {
            Device       device  = new Radio();
            Command      command = new On(device);
            DeviceButton button  = new DeviceButton(command);

            button.press();
            Assert.AreEqual(device.status, Status.on);
            button.pressUndo();
            Assert.AreEqual(device.status, Status.off);

            Command      c = new VolumeUp(device);
            DeviceButton b = new DeviceButton(c);

            b.press();
            Assert.AreEqual(11, device.volume);
            b.pressUndo();
            Assert.AreEqual(10, device.volume);
        }
コード例 #7
0
ファイル: PlayWithRemote.cs プロジェクト: Kalle-kula/Patterns
        static void Main(string[] args)
        {
            // Gets the ElectronicDevice to use:
            IElectronicDevice newDevice = TvRemote.getDevice();

            // TurnTVOn contains the command to turn on the tv
            // When execute() is called on this command object
            // it will execute the method on() in Television:
            TurnTvOn onCommand = new TurnTvOn(newDevice);

            // Calling the execute() causes on() to execute in Television
            DeviceButton onPressed = new DeviceButton(onCommand);

            // When press() is called theCommand.execute(); executes
            onPressed.press();
            //----------------------------------------------------------

            // Now when execute() is called off() of Television executes
            TurnTvOff offCommand = new TurnTvOff(newDevice);

            // Calling the execute() causes off() to execute in Television
            onPressed = new DeviceButton(offCommand);

            // When press() is called theCommand.execute(); executes
            onPressed.press();
            //----------------------------------------------------------

            // Now when execute() is called volumeUp() of Television executes
            TvTurnUpp volUppCommand = new TvTurnUpp(newDevice);

            // Calling the execute() causes volumeUp() to execute in Television
            onPressed = new DeviceButton(volUppCommand);

            // When press() is called theCommand.execute(); executes
            onPressed.press();
            onPressed.press();
            onPressed.press();
            //----------------------------------------------------------

            // Creating a TV and Radio to turn off with 1 press
            Television theTv = new Television();
            Radio theRadio = new Radio();

            // Add the Electronic Devices to a List
            List<IElectronicDevice> allDevices = new List<IElectronicDevice>();

            allDevices.Add(theTv);
            allDevices.Add(theRadio);

            // Send the List of Electronic Devices to TurnItAllOff
            // where a call to run execute() on this function will
            // call off() for each device in the list
            TurnItAllOff turnOffDevices = new TurnItAllOff(allDevices);

            // This calls for execute() to run which calls for off() to
            // run for every ElectronicDevice
            DeviceButton turnThemOff = new DeviceButton(turnOffDevices);
            turnThemOff.press();
            //----------------------------------------------------------

            /*
             * It is common to be able to undo a command in a command pattern
             * To do so, DeviceButton will have a method called undo
             * Undo() will perform the opposite action that the normal
             * Command performs. undo() needs to be added to every class
             * with an execute()
             */
            turnThemOff.pressUndo();
            onPressed.pressUndo();
            // To undo more than one command add them to a LinkedList
            // using addFirst(). Then execute undo on each item until
            // there are none left. (This is your Homework)
            Console.ReadLine();
        }