Exemplo n.º 1
0
    void Start()
    {
        Debug.Log("------------------Command Pattern--------------");
        IElectronicDevice device = TVRemove.GetDevice();

        TurnTVOn     onCommand = new TurnTVOn(device);
        DeviceButton onPressed = new DeviceButton(onCommand);

        onPressed.Press();

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

        TurnTVOff offCommand = new TurnTVOff(device);

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

        TurnVolumeUp volUpCommand = new TurnVolumeUp(device);

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

        TurnVolumeDown volDownCommand = new TurnVolumeDown(device);

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

        // -----------------------
        Television tv    = new Television();
        Radio      radio = new Radio();

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

        allDevices.Add(tv);
        allDevices.Add(radio);

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

        turnThemOff.Press();

        // -----------------------
        turnThemOff.PressUndo();
    }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // Instantiate a new television.
            Device television = new Television();

            // Create a new command to turn on the television.
            Command turnTelevisionOn = new TurnOn(television);

            // Create a new device button and set the command to
            // turn the television on.
            DeviceButton deviceButton = new DeviceButton(turnTelevisionOn);

            // Press the device button (in turn, executing the command).
            deviceButton.Press();

            // Make a new command to turn off the television, set
            // the command for the device button, and "press" it.
            Command turnTelevisionOff = new TurnOff(television);

            deviceButton.SetCommand(turnTelevisionOff);
            deviceButton.Press();

            Command turnTelevisionVolumeUp = new TurnVolumeUp(television);

            deviceButton.SetCommand(turnTelevisionVolumeUp);

            // Press the button multiple times, then "press" undo.
            deviceButton.Press();
            deviceButton.Press();
            deviceButton.Press();
            deviceButton.PressUndo();
            deviceButton.PressUndo();

            // Instantiate a radio!
            Device radio = new Radio();

            // Make a command to turn the radio off, since we can
            // just use Spotify.
            Command turnRadioOff = new TurnOff(radio);

            deviceButton.SetCommand(turnRadioOff);
            deviceButton.Press();

            Console.ReadKey();
        }