Command Pattern (6)

命令模式:

       将请求封装为对象,使得客户可以将不同的请求当做参数使用,同时可以支持Undo操作。

Command Pattern

示例代码:

using System;

using System.Text;

using System.IO;

 

namespace Hello

{

   //Interface Command

   public interface Command

   {

      void Execute();

      void Undo();

   }

   //No Command

   public class NoCommand : Command

   {

      public void Execute()

      {

       

      }

      public void Undo()

      {

     

      }

   }

   //Light Class

   public class Light

   {

      public void On()

      {

        Console.WriteLine("Light is on");

      }

      public void Off()

      {

        Console.WriteLine("Light is off");

      }

   }

   //Light On Command

   public class LightOnCommand : Command

   {

      Light light;

      public LightOnCommand(Light light)

      {

        this.light = light;

      }

      public void Execute()

      {

        light.On();

      }

      public void Undo()

      {

        light.Off();

      }

   }

   //Light Off Command

   public class LightOffCommand : Command

   {

      Light light;

      public LightOffCommand(Light light)

      {

        this.light = light;

      }

      public void Execute()

      {

        light.Off();

      }

      public void Undo()

      {

        light.On();

      }

   }

   //RemoteControl

   public class RemoteControl

   {

      Command[] onCommands;

      Command[] offCommands;

      Command undoCommand;

      public RemoteControl()

      {

        onCommands = new Command[7];

        offCommands = new Command[7];

        Command noCommand = new NoCommand();

        for (int i = 0; i < 7; i++)

        {

           onCommands[i] = noCommand;

           offCommands[i] = noCommand;

        }

        undoCommand = new NoCommand();

      }

      public void SetCommand(int slot, Command onCommand, Command offCommand)

      {

        onCommands[slot] = onCommand;

        offCommands[slot] = offCommand;

      }

      public void OnButtonWasPushed(int slot)

      {

        onCommands[slot].Execute();

        undoCommand = onCommands[slot];

      }

      public void OffButtonWasPushed(int slot)

      {

        offCommands[slot].Execute();

        undoCommand = offCommands[slot];

      }

      public void UndoButtonWasPushed()

      {

        undoCommand.Undo();

      }

      public override string ToString()

      {

        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.Append("-----Remote Control-----\n");

        for (int i = 0; i < onCommands.Length; i++)

        {

           stringBuilder.Append("[slot" + i + "] " + onCommands[i].ToString()

              " " + offCommands[i].ToString() + "\n");

        }

        return stringBuilder.ToString();

      }

   }

   class Program

   {

      static void Main(string[] args)

      {

        RemoteControl remoteControl = new RemoteControl();

        Console.WriteLine(remoteControl);

        Light light = new Light();

        LightOnCommand lightOnCommand = new LightOnCommand(light);

        LightOffCommand lightOffCommand = new LightOffCommand(light);

        remoteControl.SetCommand(3, lightOnCommand, lightOffCommand);

        remoteControl.OnButtonWasPushed(3);

        remoteControl.OffButtonWasPushed(3);

        remoteControl.UndoButtonWasPushed();

        Console.WriteLine(remoteControl);

 

        remoteControl.OnButtonWasPushed(0);

        remoteControl.OffButtonWasPushed(0);

      }

   }

}

 

posted @ 2011-10-01 13:42  Erebus_NET  阅读(196)  评论(0编辑  收藏  举报