Command Pattern 命令模式

Command Pattern: encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.

命令模式把一个请求或者操作封装到一个对象中。命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。

命令模式是对命令的封装。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。每一个命令都是一个操作:请求的一方发出请求要求执行一个操作;接收的一方收到请求,并执行操作。

Client: 生成Command,指定Invoker执行Command。

Command: 含有Receiver的引用,将Receiver的方法(一些方法)封装成一个execute()方法。成为一个Command Object。在某个Client产生,在某个Invoker执行。

Receiver: 一个普通的类。

Invoker: 执行Command的类,调用execute()方法。

1. The client creates a command object.

2. The client does a setCommand() to store the command object in the invoker.

3. The client asks the invoker to execute the command.


以遥控器控制灯为例:

定义Receiver:

public class Light {
	public void on() {
		// light on
	}
	public void off() {
		// light off
	}
}

定义命令接口:

public interface Command {
	public void execute();
	public void undo();
}
定义具体命令(Command):

public class LightOnCommand implements Command {
	private Light light;
	public LightOnCommand(Light light) {
		this.light = light;
	}
	
	@Override
	public void execute() {
		light.on();
	}
	
	@Override
	public void undo() {
		light.off();
	}
}

public class LightOffCommand implements Command {
	private Light light;
	public LightOffCommand(Light light) {
		this.light = light;
	}
	
	@Override
	public void execute() {
		light.off();
	}
	
	@Override
	public void undo() {
		light.on();
	}
}
定义Invoker:

public class RemoteControl {
	private Command onCommand;
	private Command offCommand;
	private Command undoCommand;
	public RemoteControl() {
		Command noCommand = new NoCommand();
		onCommand = noCommand;
		offCommand = noCommand;
	}
	public void setCommmand(Command onCommand, Command offCommand) {
		this.onCommand = onCommand;
		this.offCommand = offCommand;
	}
	public void onButtonPushed() {
		onCommand.execute();
		undoCommand = onCommand;
	}
	public void offButtonPushed() {
		offCommand.execute();
		undoCommand = offCommand;
	}
}

定义Client:

public class RemoteLoader {
	public static void main(String[] args) {
		Light light = new Light();
		LightOnCommand lightON = new LightOnCommand(light);
		LightOffCommand lightOFF = new LightOffCommand(light);
		
		RemoteControl control = new RemoteControl();
		control.setCommmand(lightON, lightOFF);
		control.onButtonPushed();
		control.offButtonPushed();
	}
}

如果命令包括更多的实现细节,则可把Receiver省略,生成一个self-contain的命令,内含execute()方法,便于Invoker调用。如Computer-Space模型下的Job,Task,Result。

A null object is useful when you don't have a meaningful object to return, and yet you want to remove the responsibility for handling null from the client. Instead of declaring a reference to a null, you can declare it to an object which does nothing.

应用:Remote Control

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值