0
点赞
收藏
分享

微信扫一扫

一篇文章掌握命令模式


一.案例引入

需求介绍

一篇文章掌握命令模式_设计模式

二.命令模式

1.基本介绍

一篇文章掌握命令模式_类图_02


注意命令模式必须支持撤销

小例子

一篇文章掌握命令模式_命令模式_03

2.命令模式原理类图

一篇文章掌握命令模式_类图_04

对原理类图的说明

一篇文章掌握命令模式_类图_05

3.命令模式解决上述问题的UML类图

一篇文章掌握命令模式_类图_06

4.具体代码

Command

public interface Command {
//执行动作
public void execute();

//撤销动作
public void undo();
}

LightReceiver

public class LightReceiver {
public void on(){
System.out.println("电灯打开了");
}

public void off(){
System.out.println("电灯关闭了");
}
}

LightOnCommand

public class LightOnCommand implements Command
{
LightReceiver lightReceiver;

public LightOnCommand(LightReceiver lightReceiver) {
this.lightReceiver = lightReceiver;
}

@Override
public void execute() {
lightReceiver.on();
}

@Override
public void undo() {
lightReceiver.off();
}
}

LightOffCommand

public class LightOffCommand implements Command{
LightReceiver lightReceiver;

public LightOffCommand(LightReceiver lightReceiver) {
this.lightReceiver = lightReceiver;
}

@Override
public void execute() {
lightReceiver.off();
}

@Override
public void undo() {
lightReceiver.on();
}
}

NoCommand

//没有任何命令,即空执行。用于初始化每个按钮。当调用空命令时,对象什么都不做即可。
//其实,这也是一种设计模式,可以省掉对空的判断
public class NoCommand implements Command{
@Override
public void execute() {

}

@Override
public void undo() {

}
}

RemoteController

public class RemoteController {
// 开 按钮的命令数组
Command[] onCommands;
Command[] offCommands;

//执行撤销的命令
Command undoCommand;

//构造器,完成对按钮初始化
public RemoteController(){
onCommands = new Command[5];
offCommands = new Command[5];

for(int i = 0;i < 5;i++){
onCommands[i] = new NoCommand();
offCommands[i] = new NoCommand();
}
}

//给我们的按钮设置你需要的命令
public void setCommand(int no,Command onCommand,Command offCommand){
onCommands[no] = onCommand;
offCommands[no] = offCommand;
}

//按下开的按钮怎么处理
public void onButtonWasPushed(int no){
//找到你按下的 开 的按钮,并调用对应方法
onCommands[no].execute();

//记录这次的操作,用于撤销
undoCommand = onCommands[no];
}

//按下关的按钮怎么处理
public void offButtonWasPushed(int no){
//找到你按下的 关 的按钮,并调用对应方法
offCommands[no].execute();

//记录这次的操作,用于撤销
undoCommand = offCommands[no];
}

//按下撤销按钮的处理
public void undoButtonWasPushed(){
undoCommand.undo();
}
}

Client

public class Client {
public static void main(String[] args) {
//创建电灯的对象(接收者)
LightReceiver lightReceiver = new LightReceiver();

//创建电灯相关的开关命令
LightOnCommand lightOnCommand = new LightOnCommand(lightReceiver);
LightOffCommand lightOffCommand = new LightOffCommand(lightReceiver);

//需要一个遥控器
RemoteController remoteController = new RemoteController();

//给遥控器设置命令
remoteController.setCommand(0,lightOnCommand,lightOffCommand);

//按下灯的开按钮
System.out.println("-------------开灯------------");
remoteController.onButtonWasPushed(0);

//按下灯的关按钮
System.out.println("-------------关灯------------");
remoteController.offButtonWasPushed(0);

//撤销
System.out.println("-------------撤销------------");
remoteController.undoButtonWasPushed();

/**
* -------------开灯------------
* 电灯打开了
* -------------关灯------------
* 电灯关闭了
* -------------撤销------------
* 电灯打开了
*/
}
}

5.命令模式的注意事项和细节

一篇文章掌握命令模式_java_07


举报

相关推荐

Linux简单命令(接上一篇文章)

0 条评论