命令模式: 将请求封闭成对象,这可以让你使用不同的请求,队列.或者日志请求来参数化其他对象.命令模式式可支持撤销技术操作.
实例需求:
一个摇控器.可以控制N个电器设备的开关,以及相关的操作.还可以一些组合操作.有一个撤消操作.(需求见附件)
命令接口
开关接口
实例灯
开灯命令
关灯操作
电视
//开电视
关电视
空命令
摇控器
关闭看电视组合命令
开启看电视组合命令
测试类
当需要将发出请求的对象和执行请求的对象解耦的时候,使用命令模式.
实例需求:
一个摇控器.可以控制N个电器设备的开关,以及相关的操作.还可以一些组合操作.有一个撤消操作.(需求见附件)
命令接口
public interface Command {
//执行相关命令
public void execute();
//撤消相关命令
public void undo();
}
开关接口
public interface Switch {
public void on();
public void off();
}
实例灯
public class Light implements Switch{
private String name;
public Light(String name){
this.name = name;
}
//开操作
public void on(){
System.out.println(name +" is on");
}
//关操作
public void off(){
System.out.println(name +" is off");
}
}
开灯命令
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.on();
}
@Override
//撤消操作.因为灯只有二种状态所以撤消操作就是关灯
public void undo() {
// TODO Auto-generated method stub
light.off();
}
}
关灯操作
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.off();
}
@Override
public void undo() {
// TODO Auto-generated method stub
light.on();
}
}
电视
public class TV implements Switch {
private String name;
public TV(String name) {
this.name = name;
}
@Override
public void off() {
// TODO Auto-generated method stub
System.out.println(name + " is off");
}
@Override
public void on() {
// TODO Auto-generated method stub
System.out.println(name + " is on");
}
}
//开电视
public class TVOnCommand implements Command {
private TV tv;
public TVOnCommand(TV tv) {
this.tv = tv;
}
@Override
public void execute() {
// TODO Auto-generated method stub
tv.on();
}
@Override
public void undo() {
// TODO Auto-generated method stub
tv.off();
}
}
关电视
public class TVOffCommand implements Command {
private TV tv;
public TVOffCommand(TV tv) {
this.tv = tv;
}
@Override
public void execute() {
// TODO Auto-generated method stub
tv.off();
}
@Override
public void undo() {
// TODO Auto-generated method stub
tv.on();
}
}
空命令
public class NoCommand implements Command {
@Override
public void execute() {
// TODO Auto-generated method stub
}
@Override
public void undo() {
// TODO Auto-generated method stub
}
}
摇控器
public class Controler {
//有7个电器设置控制接口
private final static int PROTNUM = 7;
//开控制命令组
private Command[] onCommands = new Command[PROTNUM];
//关控制命令组
private Command[] offCommands = new Command[PROTNUM];
private Command noCommand = new NoCommand();
//一个用于撤消的List
private static LinkedList<Command> undoList = new LinkedList<Command>();
public Controler() {
//初始化所有命令组有空命令
for (int i = 0; i < PROTNUM; i++) {
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
}
//根据接口号设置开关命令
public void setProt(int index,Command onCommand,Command offCommand){
if(index>PROTNUM)
return;
onCommands[index] = onCommand;
offCommands[index] = offCommand;
}
//根据接口号执行开命令
public void onButton(int index){
onCommands[index].execute();
//增加到撤消的List
undoList.add(onCommands[index]);
}
//根据接口号执行关命令
public void offBuffon(int index){
offCommands[index].execute();
//增加到撤消的List
undoList.add(offCommands[index]);
}
//撤消操作
public void undoButton(){
//取撤消的List的最后一个命令进行撤消
Command command = undoList.removeLast();
command.undo();
}
}
关闭看电视组合命令
public class LookTvOffCommand implements Command {
//命令组合
private Command[] commands;
public LookTvOffCommand(Command[] commands) {
this.commands = commands;
}
@Override
//执行命令组合
public void execute() {
// TODO Auto-generated method stub
for (Command command : commands) {
command.execute();
}
}
@Override
//撤消命令组合
public void undo() {
// TODO Auto-generated method stub
for (Command command : commands) {
command.undo();
}
}
}
开启看电视组合命令
public class LookTvOnCommand implements Command {
private Command[] commands;
public LookTvOnCommand(Command[] commands) {
this.commands = commands;
}
@Override
public void execute() {
// TODO Auto-generated method stub
for(Command command:commands){
command.execute();
}
}
@Override
public void undo() {
// TODO Auto-generated method stub
for(Command command:commands){
command.undo();
}
}
}
测试类
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Controler control = new Controler();
Light light = new Light("room light");
TV tv = new TV("romm tv");
//灯放在摇控器的0号口
control.setProt(0, new LightOnCommand(light), new LightOffCommand(light));
control.onButton(0);
control.offBuffon(0);
control.undoButton();
//封装看电视的组合命令(关灯,看电视)
Command[] lookTvOnCommands = {new LightOffCommand(light),new TVOnCommand(tv)};
//封装不看电视的组合命令
Command[] lookTvOffCommands = {new LightOnCommand(light),new TVOffCommand(tv)};
//看电视命令
LookTvOnCommand lookTvOnCommand = new LookTvOnCommand(lookTvOnCommands);
//不看电视命令
LookTvOffCommand lookTvOffCommand = new LookTvOffCommand(lookTvOffCommands);
control.setProt(1, lookTvOnCommand, lookTvOffCommand);
control.onButton(1);
control.offBuffon(1);
control.undoButton();
}
}
当需要将发出请求的对象和执行请求的对象解耦的时候,使用命令模式.