设计模式-命令模式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp
{
    class Program
    {
        public interface Command
        {
            void Execute();
            void Undo();
        }

        public class NoCommand : Command
        {
            public void Execute() { }
            public void Undo() { }
        }
        public class Light
        {
            public void On()
            {
                Console.WriteLine("I am ligthting!");
            }
            public void Off()
            {
                Console.WriteLine("I am off now!");
            }
        }

        public class LightOnCommand : Command
        {
            protected Light light = null;
            public LightOnCommand(Light _light)
            {
                light = _light;
            }

            public void Execute()
            {
                light.On();
            }
            public void Undo()
            {
                light.Off();
            }
        }


        public class OneButtonRemoteControlWithoutUndo
        {
            private Command slot = null;
            public OneButtonRemoteControlWithoutUndo() { }
            public void setCommand(Command Command)
            {
                slot = Command;
            }
            public void ButtonPressed()
            {
                slot.Execute();
            }
        }

        public class EightButtonsRemoteControlWithUndoButton
        {
            private     List       onCommand;
            private     List       offCommand;
            private     Command             undoCommand;

            public EightButtonsRemoteControlWithUndoButton()
            {
                onCommand = new List(new Command[7]);
                offCommand = new List(new Command[7]);

                for (int i = 0; i < 7; ++i) {
                    onCommand[i] = new NoCommand();
                    offCommand[i] = new NoCommand();
                }
                    undoCommand = new NoCommand();
            }

            public void setCommand(int slot, Command _onCommand, Command _offCommand)
            {
                onCommand[slot] = _onCommand;
                offCommand[slot] = _offCommand;
            }

            public void onButtonWasPushed(int slot)
            {
                onCommand[slot].Execute();
                undoCommand = onCommand[slot];
            }
            public void offButtonWasPushed(int slot)
            {
                offCommand[slot].Execute();
                undoCommand = offCommand[slot];
            }

            public void undoButtonWasPressed()
            {
                undoCommand.Undo();
            }

            //TODO
            //override ToString()
        }

        // Undo button in multistate situation
        public class CeilingFan
        {
            public const int                 HIGH                    = 3;
            public const int                 MEDIUM                  = 2;
            public const int                 LOW                     = 1;
            public const int                 OFF                     = 0;

            private int speed;
            private string location;

            public CeilingFan(string _location)
            {
                location = _location;
                speed = OFF;
            }

            public int Speed
            {
                get
                {
                    return speed;
                }
            }

            public void High()
            {
                speed = HIGH;
                Console.WriteLine("I am high speed now!");
            }
            public void Medium()
            {
                speed = MEDIUM;
                Console.WriteLine("I am medium speed now!");
            }
            public void Low()
            {
                speed = LOW;
                Console.WriteLine("I am low speed now!");
            }
            public void Off()
            {
                speed = OFF;
                Console.WriteLine("I am off now!");
            }
        }

        public class CeilingFanHighCommand : Command
        {
            private CeilingFan ceilingFan;
            private int preSpeed;

            public CeilingFanHighCommand(CeilingFan _ceilingFan)
            {
                ceilingFan = _ceilingFan;
            }

            public void Execute()
            {
                preSpeed = ceilingFan.Speed;
                ceilingFan.High();
            }
            
            public void Undo ()
            {
                switch (preSpeed) {
                    case 3:
                        break;
                    case 2:
                        ceilingFan.Medium();
                        break;
                    case 1:
                        ceilingFan.Low();
                        break;
                    case 0:
                        ceilingFan.Off();
                        break;
                    default:
                        throw new ArgumentOutOfRangeException 
                            (" speed degree error!");
                }
            }
        }
        public static void Test()
        {
            Light light = new Light();
            LightOnCommand lightOnCommand =
                new LightOnCommand(light);

            OneButtonRemoteControlWithoutUndo oneButtonRemote =
                new OneButtonRemoteControlWithoutUndo();

            oneButtonRemote.setCommand(lightOnCommand);
            oneButtonRemote.ButtonPressed();

            CeilingFan ceilingFan = new CeilingFan("Living room");

            CeilingFanHighCommand ceilingFanHighCommand =
                new CeilingFanHighCommand(ceilingFan);

            EightButtonsRemoteControlWithUndoButton remote =
                new EightButtonsRemoteControlWithUndoButton();

            remote.setCommand(0, ceilingFanHighCommand, ceilingFanHighCommand);
            remote.onButtonWasPushed(0);
            remote.undoButtonWasPressed();
        }
        static void Main(string[] args)
        {
            Test();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值