命令模式(行为型)

命令模式设计详解

命令模式

    将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。

 适用性

    1.抽象出待执行的动作以参数化某个对象。
    2.在不同的时刻指定、排列和执行请求。
    3.支持取消操作。
    4.支持修改日志,这样当系统崩溃时,修改可以被重做一遍。
    5.用构建在原语操作上的高层操作构造一个系统。
结构类图

headfirst结构类图

-----------------------------------------------------------------
系统结构图

----------------------------------------------------------------------------------------------------------------------
抽象命令
---------------------------------------------------------------------------
public interface Command {
	public void execute();
}
---------------------------------------------------------------------------
具体命令
---------------------------------------------------------------------------

public class DoorOffCommand implements Command {
	private Door door;


	public DoorOffCommand(Door door) {
		this.door = door;
	}
	@Override
	public void execute() {
		// TODO Auto-generated method stub
		door.doorOff();
	}
}
public class DoorOnCommand implements Command{
	private Door door;


	public DoorOnCommand(Door door) {
		this.door = door;
	}
	@Override
	public void execute() {
		// TODO Auto-generated method stub
		door.doorOn();
	}
}
public class LightOffCommand implements Command {
	private Light light;


	public LightOffCommand(Light light) {
		this.light = light;
	}


	@Override
	public void execute() {
		// TODO Auto-generated method stub
		light.lightOff();
	}
}
public class LightOnCommand implements Command{
	private Light light;


	public LightOnCommand(Light light) {
		this.light = light;
	}
	@Override
	public void execute() {
		// TODO Auto-generated method stub
		light.lightOn();
	}
}


---------------------------------------------------------------------------
命令控制器
---------------------------------------------------------------------------------------
public class SimpleRemoteControl {
	private Command[] onCommand;
	private Command[] offCommand;
	private Command undocommand;
	
	public SimpleRemoteControl(){
		onCommand=new Command[2];
		offCommand=new Command[2];
		
	}
	public void setCommand(int slot,Command onCommand,Command offCommand){
		this.onCommand[slot]=onCommand;
		this.offCommand[slot]=offCommand;
	}	
	public void buttonDoPress(int slot){
		onCommand[slot].execute();
		undocommand=onCommand[slot];
	}
	public void buttonOffDoPress(int slot){
		offCommand[slot].execute();
		undocommand=onCommand[slot];
	}	
	public void unDoCommand(){
		undocommand.execute();
	}
}

---------------------------------------------------------------------------------------
命令执行者
---------------------------------------------------------------------------

public class Door {
	public Door(){
		
	}
	public void doorOff(){
		System.out.println("Door off");
	}
	public void doorOn(){
		System.out.println("Door on");
	}
}
public class Light {
	public Light(){
		
	}
	public void lightOn(){
		System.out.println("light on");
	}
	public void lightOff(){
		System.out.println("light off");
	}
}


---------------------------------------------------------------------------
客户
---------------------------------------------------------------------------
public class TestClient {


	public static void main(String[] args) {
		Door door=new Door();
		Light light=new Light();
		Command command=new DoorOffCommand(door);
		Command command2=new DoorOnCommand(door);
		
		Command command3=new LightOffCommand(light);
		Command command4=new LightOnCommand(light);
		SimpleRemoteControl simpleRemoteControl =new SimpleRemoteControl();
		
		simpleRemoteControl.setCommand(0, command, command2);
		simpleRemoteControl.setCommand(1, command3, command4);
		System.out.println("--------------");
		simpleRemoteControl.buttonDoPress(0);
		simpleRemoteControl.buttonOffDoPress(0);
		simpleRemoteControl.unDoCommand();
		System.out.println("--------------");
		simpleRemoteControl.buttonDoPress(1);
		simpleRemoteControl.buttonOffDoPress(1);
		simpleRemoteControl.unDoCommand();
		System.out.println("--------------");
	}
}

---------------------------------------------------------------------------
执行结果
---------------------------------------------------------------------------------------
--------------
Door off
Door on
Door off
--------------
light off
light on
light off
--------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值