需求如下:现在想做一个万能遥控器,可以遥控电视机:打开、关闭、换频道;可以遥控CD机:打开、关闭;可以遥控厂门等;以下图遥控器为例:

代码如下:

 public interface ICommand
    {
       void Excute();
       void Cancle();
    }

 public  class OnWatchTVCommand:ICommand
    {
       WatchTV watchTv;

       public OnWatchTVCommand(WatchTV _watchTv)
       {
           watchTv = _watchTv;
       }
        public void Excute()
        {
            watchTv.On();
            watchTv.ChangeChannel();
        }

        public void Cancle()
        {
            watchTv.Off();
        }
    }


 public class OffWatchTVCommand : ICommand
    {
        WatchTV watchTv;
        public OffWatchTVCommand(WatchTV _watchTv)
        {
            watchTv = _watchTv;
        }
        public void Excute()
        {
            watchTv.Off();
        }

        public void Cancle()
        {
            watchTv.On();
        }
    }

  public class OnCDCommand : ICommand
        {
            CD _cD;

            public OnCDCommand(CD cd)
            {
                _cD = cd;
            }
            public void Excute()
            {
                _cD.On();
            }

            public void Cancle()
            {
                _cD.OFF();
            }
        }

 public class OFFCDCommand : ICommand
    {

        CD _cD;
        public OFFCDCommand(CD cd)
        {
            _cD = cd;
        }
        public void Excute()
        {
            _cD.OFF();

        }

        public void Cancle()
        {
            _cD.On();
        }

    }

 public class CD
    {
      public string On()
      {
          return ("打开CD");
      }
      public string OFF()
      {
          return ("关闭CD");
      }

    }


 public class WatchTV
    {

       public string On()
       {
         return ("打开电视机!");
       }
       public string Off()
       {
           return ("关闭电视机!");
       }

       public string ChangeChannel()
       {
           return ("切换电视机频道!");
       }
    }
public class RemoteControl
    {
      //打开命令数组
      ICommand[] OnCommands;
      //关闭命令数组
      ICommand[] OFFCommands;
      //撤销命令
      ICommand CancleCommand;
      public RemoteControl()
      {
          OnCommands = new ICommand[2];
          OFFCommands = new ICommand[2];
          NoCommand noCommand = new NoCommand();
          for (int i = 0; i < 2; i++)
          {
              OnCommands[i] = noCommand;
              OFFCommands[i] = noCommand;
          }
          CancleCommand = noCommand;
      }

      /// <summary>
      /// 设置命令
      /// </summary>
      /// <param name="port">按钮编号</param>
      /// <param name="OnCommand">开启命令</param>
      /// <param name="OffCommand">关闭命令</param>
      public void SetCommand(int port,ICommand OnCommand,ICommand OffCommand)
      {
          OnCommands[port] = OnCommand;
          OFFCommands[port] = OffCommand;

      }

      public void OnButtonWasPushed(int port)
      {
          OnCommands[port].Excute();
          CancleCommand= OnCommands[port];
      }
      public void OFFButtonWasPushed(int port)
      {
          OFFCommands[port].Excute();
          CancleCommand= OFFCommands[port];
      }

      public void Cancle()
      {
          CancleCommand.Cancle();
      }

    }


//测试:
public partial class Form1 : Form
    {
        RemoteControl c;
        OnWatchTVCommand onWatchCommand;
        OffWatchTVCommand offWatchCommand ;
        OnCDCommand onCDCommnand;
        OFFCDCommand offCDCommand;
        public Form1()
        {
            InitializeComponent();
            c = new RemoteControl();
            WatchTV watchTv = new WatchTV();
            onWatchCommand = new OnWatchTVCommand(watchTv);
            offWatchCommand = new OffWatchTVCommand(watchTv);
            CD cd = new CD();
            onCDCommnand = new OnCDCommand(cd);
            offCDCommand = new OFFCDCommand(cd);
            c.SetCommand(0, onWatchCommand, offWatchCommand);
            c.SetCommand(1, onCDCommnand, offCDCommand);
        }
        RemoteControl remoteControl = new RemoteControl();
        private void button1_Click(object sender, EventArgs e)
        {
            c.OnButtonWasPushed(0);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            c.OFFButtonWasPushed(0);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            c.Cancle();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            c.OnButtonWasPushed(1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            c.OFFButtonWasPushed(1);
        }
    }
01-03 05:54