1.概念:
将请求封装成“对象”,以便使用不同的请求,队列,或者日志来参数化其它对象。命令模式也支持可撤销操作。
自己理解:在特定接收者上绑定一组动作来封装一个请求(成为对象),并只暴露出一个execute()方法。然后将该对象做为参数传给其它类或对象(调用者),当调用者调用execute()的时候,接收者就会进行相应的动作。
2.作用
一个命令对象通过在特定接收者上绑定一组动作来封装一个请求。要达到这一点,命令对象将动作和接收者包进对象中。这个对象只暴露出一个execute()方法,当此方法被调用的时候,接收者就会进行这些动作。
要点:
(1)命令模式将发出请求的对象和执行请求的对象解耦。
(2)在被解耦的两者之间是通过命令对象进行沟通的。命令对象封装了接收者和一个或一组对作。
(3)调用者通过调用命令对象的exceute()发出请求,这会使得接收者的动作被调用。
(4)调用者会接收命令做为参数,甚至在运行时动态的进行。
3.依赖原则
(1)封装变化
(2)多用组合,少用继承
(3)针对接口编程,而不是具体的实现
(4)为交互对象之间的松耦合设计而努力
(5)类应该对扩展开放,对修改关闭
(6)依赖抽象,不依赖具体类
4.具体实现
4.1 类图
4.2 代码实现
//测试类,代表客户
package test;
import invoke.RemoteControl;
import receiver.GarageDoor;
import receiver.Light;
import command.GarageDoorCloseCommand;
import command.GarageDoorOpenCommand;
import command.LightOffCommand;
import command.LightOnCommand;
public class RemoteLoader {
public static void main(String args[]){
RemoteControl remoteControl = new RemoteControl();
Light light = new Light();
LightOnCommand lightOnCommand = new LightOnCommand(light);
LightOffCommand lightOffCommand = new LightOffCommand(light);
remoteControl.setCommand(0, lightOnCommand, lightOffCommand);
remoteControl.onButtonwasPressed(0);
remoteControl.offButtonwasPressed(0);
GarageDoor garageDoor = new GarageDoor();
GarageDoorOpenCommand garageDoorOpenCommand = new GarageDoorOpenCommand(garageDoor);
GarageDoorCloseCommand garageDoorCloseCommand = new GarageDoorCloseCommand(garageDoor);
remoteControl.setCommand(1, garageDoorOpenCommand, garageDoorCloseCommand);
remoteControl.onButtonwasPressed(1);
remoteControl.offButtonwasPressed(1);
}
}
//接收者
package receiver;
public class GarageDoor {
public GarageDoor() {
}
public void open() {
System.out.println("grageDoor is open");
}
public void close() {
System.out.println("grageDoor is close");
}
}
//接收者
package receiver;
public class Light {
public Light() {
}
public void on() {
System.out.println("light is on");
}
public void off() {
System.out.println("light is off");
}
}
//调用者
package invoke;
import command.Command;
import command.NoCommand;
public class RemoteControl {
Command[] onCommands;
Command[] offCommands;
public RemoteControl(){
onCommands = new Command[7];
offCommands = new Command[7];
for(int i=0;i<7;i++){
onCommands[i] = new NoCommand();
offCommands[i] = new NoCommand();
}
}
public void setCommand(int i ,Command onCommand,Command offCommand) {
onCommands[i] = onCommand;
offCommands[i] = offCommand;
}
public void onButtonwasPressed(int i) {
onCommands[i].execute();
}
public void offButtonwasPressed(int i){
offCommands[i].execute();
}
}
//命令对象:封装接收者和执行动作。
package command;
public interface Command {
public void execute();
}
package command;
import receiver.GarageDoor;
public class GarageDoorCloseCommand implements Command {
GarageDoor garageDoor;
public GarageDoorCloseCommand(GarageDoor garageDoor) {
this.garageDoor = garageDoor;
}
@Override
public void execute() {
garageDoor.close();
}
}
package command;
import receiver.GarageDoor;
public class GarageDoorOpenCommand implements Command {
GarageDoor garageDoor;
public GarageDoorOpenCommand(GarageDoor garageDoor) {
this.garageDoor = garageDoor;
}
@Override
public void execute() {
garageDoor.open();
}
}
package command;
import receiver.Light;
public class LightOffCommand implements Command {
Light light;
public LightOffCommand(Light light) {
this.light = light;
}
public void execute() {
light.off();
}
}
package command;
import receiver.Light;
public class LightOnCommand implements Command {
Light light;
public LightOnCommand(Light light) {
this.light = light;
}
public void execute() {
light.on();
}
}
package command;
public class NoCommand implements Command {
@Override
public void execute() {
System.out.println("no");
}
}