zoukankan      html  css  js  c++  java
  • 设计模式

    命令模式(command pattern) 多命令 具体解释


    本文地址: http://blog.csdn.net/caroline_wendy


    參考命令模式: http://blog.csdn.net/caroline_wendy/article/details/31379977


    具体步骤:

    1. 多命令, 把未使用的命令, 初始化为空对象(NoCommand), 依据參数(slot), 选择输出命令.

    /**
     * @time 2014年6月16日
     */
    package command;
    
    /**
     * @author C.L.Wang
     *
     */
    public class RemoteControl {
    
    	Command[] onCommands; //开
    	Command[] offCommands; //关
    	
    	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;
    		}
    		
    	}
    	
    	public void setCommand (int slot, Command onCommand, Command offCommand) {
    		this.onCommands[slot] = onCommand;
    		this.offCommands[slot] = offCommand;
    	}
    	
    	public void onButtonWasPushed(int slot) {
    		onCommands[slot].execute();
    	}
    	
    	public void offButtonWasPushed(int slot) {
    		offCommands[slot].execute();
    	}
    	
    	public String toString() {
    		StringBuffer stringBuffer = new StringBuffer();
    		stringBuffer.append("
    ------ Remote Control ------
    ");
    		for (int i=0; i<onCommands.length; ++i) {
    			stringBuffer.append("[slot " + i + "] " + onCommands[i].getClass().getName()
    				+ "    " + offCommands[i].getClass().getName() + "
    ");
    		}
    		
    		return stringBuffer.toString();
    	}
    }
    

    2. 空命令(NoCommand)对象, 继承命令接口.

    package command;
    
    public class NoCommand implements Command {
    	public void execute() { }
    }
    

    3. 測试对象, 分别给多命令赋值(setCommand), 通过參数(slot)调用.

    /**
     * @time 2014年6月16日
     */
    package command;
    
    import javax.crypto.spec.IvParameterSpec;
    
    /**
     * @author C.L.Wang
     *
     */
    public class RemoteLoader {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		RemoteControl remoteControl = new RemoteControl();
    		
    		Light livingRoomLight = new Light("Living Room");
    		Light kitchenLight = new Light("Kitchen");
    		CeilingFan ceilingFan = new CeilingFan("Living Room");
    		GarageDoor garageDoor = new GarageDoor("");
    		Stereo stereo = new Stereo("Living Room");
    		
    		LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);
    		LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);
    		LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight);
    		LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight);
    		
    		CeilingFanOnCommand ceilingFanOn = new CeilingFanOnCommand(ceilingFan);
    		CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan);
    		
    		GarageDoorOnCommand garageDoorOn = new GarageDoorOnCommand(garageDoor);
    		GarageDoorOffCommand garageDoorOff = new GarageDoorOffCommand(garageDoor);
    		
    		StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo);
    		StereoOffCommand stereoOffCommand = new StereoOffCommand(stereo);
    		
    		remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
    		remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);
    		remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff);
    		remoteControl.setCommand(3, stereoOnWithCD, stereoOffCommand);
    		
    		System.out.println(remoteControl);
    		
    		remoteControl.onButtonWasPushed(0);
    		remoteControl.offButtonWasPushed(0);
    		remoteControl.onButtonWasPushed(1);
    		remoteControl.offButtonWasPushed(1);
    		remoteControl.onButtonWasPushed(2);
    		remoteControl.offButtonWasPushed(2);
    		remoteControl.onButtonWasPushed(3);
    		remoteControl.offButtonWasPushed(3);
    	}
    
    }
    


    4. 输出:

    ------ Remote Control ------
    [slot 0] command.LightOnCommand    command.LightOffCommand
    [slot 1] command.LightOnCommand    command.LightOffCommand
    [slot 2] command.CeilingFanOnCommand    command.CeilingFanOffCommand
    [slot 3] command.StereoOnWithCDCommand    command.StereoOffCommand
    [slot 4] command.NoCommand    command.NoCommand
    [slot 5] command.NoCommand    command.NoCommand
    [slot 6] command.NoCommand    command.NoCommand
    
    Living Room Light is on
    Living Room Light is off
    Kitchen Light is on
    Kitchen Light is off
    Living Room ceiling fan is on high
    Living Room ceiling fan is off
    Living Room stereo is on
    Living Room stereo is set for CD input
    Living Room Stereo volume set to 11
    Living Room stereo is off
    



    其余代码下载: http://download.csdn.net/detail/u012515223/7506695






  • 相关阅读:
    @Value注释失效
    405不支持post请求
    AutoComplete="off"取消input记住之前输入过的内容
    maven解决架包冲突
    HTTP请求中 request payload 和 formData 区别?
    图片视频预览直接将请求下载的url(返回为流)操作和显示
    vue标签属性拼接变量的写法
    计算机中丢失MSVCP120.dll,Photoshopcc2017无法启动(免安装,绿色版本)
    element--循环多个表单,保存和重置怎么办?
    vue---局部刷新和刷新页面的
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5341900.html
Copyright © 2011-2022 走看看