命令模式就是通过封装方法调用,我们可以把类装成形,调用此类的对象,我们不需要关心此对象里面是如何运作的

代码如下简单的命令模式:

  /***********************************simple命令模式**************************************/
    //创建灯开关类
    public class Light
    {
        string location;
        public Light(string location)
        {
            this.location = location;
        }
        public string On()
        {
            return location + " is on";

        }
        public string Off()
        {
            return location + " is off";
        }
    }
    //创建接口命令
    public interface Command
    {
         object execute();
    }
    //创建灯开关命令
    public class LightOnCommand:Command
    {
        Light light;

        public LightOnCommand(Light light)
        {
            this.light = light;
        }
        public object execute()
        {
            return light.On();
        }
    }
    //使用命令对象
    public class SimpleLightControl
    {
        Command cmd;

        public SimpleLightControl(){}

        public void setCommand(Command command)
        {
            cmd = command;
        }
        public string buttonWasPressed()
        {
           return cmd.execute().ToString();
        }
    }

   
 

使用该方法

    public class LightControlTest
    {
        public static void Main()
        {
            string lightWas;
            SimpleLightControl lightControl = new SimpleLightControl();//调用者
            Light light = new Light("door");//对象
            LightOnCommand lightCmd = new LightOnCommand(light);//请求命令接受对象
            lightControl.setCommand(lightCmd);//把命令传给调用者
            lightWas = lightControl.buttonWasPressed();//模拟按钮按下
            Console.WriteLine(lightWas);
        }
    }
    public class LightControlTest
    {
        
public static void Main()
        {
            
string lightWas;
            SimpleLightControl lightControl 
= new SimpleLightControl();//调用者
            Light light = new Light("door");//对象
            LightOnCommand lightCmd = new LightOnCommand(light);//请求命令接受对象
            lightControl.setCommand(lightCmd);//把命令传给调用者
            lightWas = lightControl.buttonWasPressed();//模拟按钮按下
            Console.WriteLine(lightWas);
        }
    }

 

 /***********************************复杂点命令模式 *********************************************/

//创建接口命令 

 //创建接口命令
    public interface Command
    {
         object execute();
    }

 

//创建无命令类

 

  public class NoCommand:Command
    {
        public NoCommand()
        { }

        #region Command Members
        public object execute()
        {
            return null;
        }
        #endregion
    }

 

//创建灯开关类


   //创建灯开关类
    public class Light
    {
        string location;
        public Light(string location)
        {
            this.location = location;
        }
        public string On()
        {
            return location + " is on";

        }
        public string Off()
        {
            return location + " is off";
        }
    }

   //创建灯开关命令

    public class LightOnCommand:Command
    {
        Light light;

        public LightOnCommand(Light light)
        {
            this.light = light;
        }
        public object execute()
        {
            return light.On();
        }
    }
    //创建关灯命令
    public class LightOffCommand:Command
    {
        Light light;

        public LightOffCommand(Light light)
        {
            this.light = light;
        }
        public object execute()
        {
            return light.Off();
        }
    }

 

 // 车库门

//车库门
    public class GarageDoor
    {
        string location;
        public GarageDoor(string location)
        {
            this.location = location;
        }

        public string Up()
        {
            return location + " is up";
        }

        public string Down()
        {
            return location + " is down";
        }

        public string Stop()
        {
            return location + " movement is stopped";
        }

        public string LightOn()
        {
            return location + " light is on";
        }

        public string LightOff()
        {
            return location + " light is off";
        }
    }

    //车库门下
    public class GarageDoorDownCommand:Command
    {
        GarageDoor garageDoor;

        public GarageDoorDownCommand(GarageDoor garageDoor)
        {
            this.garageDoor = garageDoor;
        }

        #region Command Members
        public object execute()
        {
            return garageDoor.Down();
        }
        #endregion
    }
    //车库门上
    public class GarageDoorUpCommand:Command
    {
        GarageDoor garageDoor;

        public GarageDoorUpCommand(GarageDoor garageDoor)
        {
            this.garageDoor = garageDoor;
        }

        #region Command Members

        public object execute()
        {
            return garageDoor.Up();
        }

        #endregion
    }

 

 //创建控制命令

    public class ControlCommand
    {
        Command[] onCommands;
        Command[] offCommands;

       
        public ControlCommand()
        {
            onCommands = new Command[2];
            offCommands = new Command[2];
            Command noCommand = new NoCommand();
            for (int i=0;i<2;i++)
            {
                onCommands[i] = noCommand;
                offCommands[i] = noCommand;
            }
        }

        public void SetCommand(int slot,Command onCommand,Command offCommand)
        {
            onCommands[slot] = onCommand;
            offCommands[slot] = offCommand;
        }

        public object OnButtonWasPushed(int slot)
        {
            return onCommands[slot].execute();
        }

        public object OffButtonWasPushed(int slot)
        {
            return offCommands[slot].execute();
        }

        public string toString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("\n------ Remote Control -------\n");
            for (int i = 0; i < onCommands.Length; i++)
            {
                sb.Append("[slot " + i + "] " + onCommands[i].GetType().Name
                    + "    " + offCommands[i].GetType().Name + "\n");
            }
            return sb.ToString();
        }
    }

 

 //调用命令模式

  public class ControlCommandTest
    {
        public static void Main()
        {
            Light light = new Light("light");//创建灯
            LightOnCommand lightOnCmd = new LightOnCommand(light);
            LightOffCommand lightOffCmd = new LightOffCommand(light);

            GarageDoor door = new GarageDoor("Garage door");
            GarageDoorDownCommand doorDownCmd = new GarageDoorDownCommand(door);
            GarageDoorUpCommand doorUpCmd = new GarageDoorUpCommand(door);

            ControlCommand controlCmd = new ControlCommand();
            controlCmd.SetCommand(0, lightOnCmd, lightOffCmd);
            controlCmd.SetCommand(1,  doorDownCmd,doorUpCmd);

            Console.WriteLine(controlCmd.OnButtonWasPushed(0).ToString());
            Console.WriteLine(controlCmd.OffButtonWasPushed(0).ToString());
            Console.WriteLine(controlCmd.OnButtonWasPushed(1).ToString());
            Console.WriteLine(controlCmd.OffButtonWasPushed(1).ToString());

            Console.WriteLine(controlCmd.toString());

        }
    }

 

 

 

 

 

 

    

 

posted on 2011-01-25 16:07  jackdesk  阅读(151)  评论(0编辑  收藏  举报