设计模式: 自己手动写一个命令模式

.命令模式: 将“请求”封装成对象,以便使用不同的请求、队列、日志来参数化其他对象。命令模式也支持可撤销的操作。

  命令对象将动作和接收者(当作的执行者)包进对象中。这个对象之暴露一个execute()方法. 命令也可以用来实现日志和事务系统。宏命令是命令的一种简单延伸,允许调用多个命令。


下面是命令模式的一个类图:



一个案例的源代码如下:

package command;
/**
 * 命令对象接口
 * @author Arvon
 *
 */
public interface Command {
	public void execute();
	public void undo();
}

package command;

public class LightOnCommand implements Command {
	private Light light;
	
	
	public LightOnCommand(Light light) {
		super();
		this.light = light;
	}

	@Override
	public void execute() {
		// TODO Auto-generated method stub
		light.on();
	}

	@Override
	public void undo() {
		// TODO Auto-generated method stub
		light.off();
	}

}

package command;

public class LightOffCommand implements Command {
	private Light light;
	
	public LightOffCommand(Light light) {
		super();
		this.light = light;
	}

	@Override
	public void execute() {
		// TODO Auto-generated method stub
		light.off();

	}

	@Override
	public void undo() {
		// TODO Auto-generated method stub
		light.on();

	}

}


package command;

public class CeillingFanHighCommand implements Command {

	private CeilingFan fan;
	private int prevSpeed;
	@Override
	public void execute() {
		prevSpeed = fan.getSpeed();
		fan.high();
	}
	
	

	public CeillingFanHighCommand(CeilingFan fan) {
		super();
		this.fan = fan;
	}



	@Override
	public void undo() {
		if (prevSpeed == CeilingFan.HIGH) {
			fan.high();
		} else if (prevSpeed == CeilingFan.MEDIUM) {
			fan.medium();
		} else if (prevSpeed == CeilingFan.LOW) {
			fan.low();
		} else if (prevSpeed == CeilingFan.OFF) {
			fan.off();
		}
		

	}

}

package command;

public class CeillingFanOffCommand implements Command {
	CeilingFan ceilingFan;
	int prevSpeed;
  
	public CeillingFanOffCommand(CeilingFan ceilingFan) {
		this.ceilingFan = ceilingFan;
	}
	@Override
	public void execute() {
		// TODO Auto-generated method stub
		prevSpeed = ceilingFan.getSpeed();
		ceilingFan.off();

	}

	@Override
	public void undo() {
		// TODO Auto-generated method stub
		if (prevSpeed == CeilingFan.HIGH) {
			ceilingFan.high();
		} else if (prevSpeed == CeilingFan.MEDIUM) {
			ceilingFan.medium();
		} else if (prevSpeed == CeilingFan.LOW) {
			ceilingFan.low();
		} else if (prevSpeed == CeilingFan.OFF) {
			ceilingFan.off();
		}
	}

}


package command;
/**
 * 什么都不做 空对象
 * @author Administrator
 *
 */
public class NoCommand implements Command {

	@Override
	public void execute() {
		// TODO Auto-generated method stub

	}

	@Override
	public void undo() {
		// TODO Auto-generated method stub

	}

}



package command;
/**
 * 遥控器 Invoker 管理许多command对象
 * @author Arvon
 *
 */
public class RemoteControl {
	Command[] onCommands;
	Command[] offCommands;
	/**
	 * 记录前一个命令
	 */
	Command undoCommand;
	/**
	 * 初始化所有命令
	 */
	public RemoteControl() {
		super();
		onCommands = new Command[5];
		offCommands = new Command[5];
		for(int i=0;i<5;i++){
			Command noCommand = new NoCommand();
			onCommands[i] = noCommand ;
			offCommands[i] = noCommand ;
		}
			
	}
	/**
	 * 设置命令
	 * @param slot 第几个插槽
	 * @param onCommand
	 * @param offCommand
	 */
	public void setCommand(int slot,Command onCommand, Command offCommand){
		onCommands[slot] = onCommand;
		offCommands[slot] = offCommand;
	}
	
	public void onButtonWasPush(int slot){
		onCommands[slot].execute();
		undoCommand = onCommands[slot];
		
	}
	public void offButtonWasPush(int slot){
		offCommands[slot].execute();
		undoCommand = offCommands[slot];
	}
	
	public void undoButtonWasPush(){
		if(undoCommand!=null)
			undoCommand.undo();
		
	}
	
	public String toString() {
		StringBuffer stringBuff = new StringBuffer();
		stringBuff.append("\n------ Remote Control -------\n");
		for (int i = 0; i < onCommands.length; i++) {
			stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getSimpleName()
				+ "    " + offCommands[i].getClass().getSimpleName() + "\n");
		}
		stringBuff.append("[undo] " + undoCommand.getClass().getSimpleName() + "\n");
		return stringBuff.toString();
	}
	
	
	
}


package command;
/**
 * 灯 命令的执行者
 * @author Administrator
 *
 */
public class Light {

	public void on() {
		// TODO Auto-generated method stub
		System.out.println("light is on...");
		
	}

	public void off() {
		// TODO Auto-generated method stub
		System.out.println("light is off...");
	}
	
}


package command;
/**
 * 电风扇 命令的执行者
 * @author Administrator
 *
 */
public class CeilingFan {

	public static final int HIGH = 3;
	public static final int MEDIUM = 2;
	public static final int LOW = 1;
	public static final int OFF = 0;
	String location;
	int speed;
 
	public CeilingFan(String location) {
		this.location = location;
		speed = OFF;
	}
  
	public void high() {
		speed = HIGH;
		System.out.println(location + " ceiling fan is on high");
	} 
 
	public void medium() {
		speed = MEDIUM;
		System.out.println(location + " ceiling fan is on medium");
	}
 
	public void low() {
		speed = LOW;
		System.out.println(location + " ceiling fan is on low");
	}
  
	public void off() {
		speed = OFF;
		System.out.println(location + " ceiling fan is off");
	}
  
	public int getSpeed() {
		return speed;
	}

}


package command;
/**
 * 测试类
 * @author Administrator
 *
 */
public class CommandTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		RemoteControl control = new RemoteControl();
		 Light light = new Light();
		 Command lightOn = new LightOnCommand(light);
		 Command lightOff = new LightOffCommand(light);
		 control.setCommand(0, lightOn, lightOff );
		 control.onButtonWasPush(0);
		 control.offButtonWasPush(0);
		 control.undoButtonWasPush();
		 CeilingFan fan = new CeilingFan("living room");
		 Command mCeillingFanHighCommand = new CeillingFanHighCommand(fan);
		 Command CeillingFanOffCommand = new CeillingFanOffCommand(fan);
		 control.setCommand(1, mCeillingFanHighCommand, CeillingFanOffCommand);
		 control.onButtonWasPush(1);
		 control.offButtonWasPush(1);
		 control.onButtonWasPush(1);
		 control.undoButtonWasPush();
		 
	}

}


程序的输出:

light is on...
light is off...
light is on...
living room ceiling fan is on high
living room ceiling fan is off
living room ceiling fan is on high
living room ceiling fan is off



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值