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






  • 相关阅读:
    Celery详解
    JWT详解
    进程及进程池
    多线程详解
    python常用模块之os模块的用法
    python常用模块之paramiko与ssh
    reflect 反射
    http 静态文件
    模板渲染语言
    http web 开发
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5341900.html
Copyright © 2011-2022 走看看