将一个请求封装为一个对象,使发出请求的责任和执行请求的责任分割开。
四个角色
- Invoker:调用者角色
- Command:命令角色,一般为接口或抽象类
- Receiver:接受者角色
- ConcreteCommand:将接受者对象与一个动作绑定,调用接受者相应的操作
package com.strife.pattern.command;
/**
* 命令模式
* 遥控器
* @author mengzhenghao
* @date 2022/5/30
*/
public class CommandCode {
/** 假设遥控器上有5组按钮 */
public static final int NUMS = 5;
public static void main(String[] args) {
LightReceiver receiver = new LightReceiver();
Command onCommand = new LightOnCommand(receiver);
Command offCommand = new LightOffCommand(receiver);
TvReceiver tvReceiver = new TvReceiver();
Command onTvCommand = new TvOnCommand(tvReceiver);
Command offTvCommand = new TvOffCommand(tvReceiver);
RemoteController remoteController = new RemoteController();
remoteController.setOnCommands(0, onCommand, offCommand);
remoteController.setOnCommands(1, onTvCommand, offTvCommand);
System.out.println("------------按下电灯开按钮-----------");
remoteController.onButtonWasPushed(0);
System.out.println("------------按下电灯关按钮-----------");
remoteController.offButtonWasPushed(0);
System.out.println("------------按下电灯关按钮-----------");
remoteController.undoButtonWasPushed();
System.out.println("------------按下电视开按钮-----------");
remoteController.onButtonWasPushed(1);
System.out.println("------------按下电视关按钮-----------");
remoteController.offButtonWasPushed(1);
System.out.println("------------按下电视关按钮-----------");
remoteController.undoButtonWasPushed();
}
}
/** 命令角色:Command */
interface Command {
/** 执行任务 */
void execute();
/** 撤销 */
void undo();
}
/** 具体命令角色类:ConcreteCommand */
class LightOnCommand implements Command {
private final LightReceiver receiver;
public LightOnCommand(LightReceiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
receiver.on();
}
@Override
public void undo() {
receiver.off();
}
}
/** 具体命令角色类:ConcreteCommand */
class TvOnCommand implements Command {
private final TvReceiver receiver;
public TvOnCommand(TvReceiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
receiver.on();
}
@Override
public void undo() {
receiver.off();
}
}
/** 具体命令角色类:ConcreteCommand */
class LightOffCommand implements Command {
private final LightReceiver receiver;
public LightOffCommand(LightReceiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
receiver.off();
}
@Override
public void undo() {
receiver.on();
}
}
/** 具体命令角色类:ConcreteCommand */
class TvOffCommand implements Command {
private final TvReceiver receiver;
public TvOffCommand(TvReceiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
receiver.off();
}
@Override
public void undo() {
receiver.on();
}
}
/**
* 没有任何命令,即空执行,用于初始化每个按钮
* 当调用空命令时,对象什么都不做,可以省去空命令
*/
class NoCommand implements Command {
@Override
public void execute() {
}
@Override
public void undo() {
}
}
/** 接收者角色类:Receiver */
class LightReceiver {
/** 执行命令 */
public void on() {
System.out.println("电灯打开了……");
}
public void off() {
System.out.println("电灯关闭了……");
}
}
/** 接收者角色类:Receiver */
class TvReceiver {
/** 执行命令 */
public void on() {
System.out.println("电视打开了……");
}
public void off() {
System.out.println("电视关闭了……");
}
}
/** 请求者角色类:Invoker */
class RemoteController {
/** 开按钮组 */
Command[] onCommands;
/** 关按钮组 */
Command[] offCommands;
/** 撤销按钮 */
Command undoCommand;
public RemoteController() {
onCommands = new Command[CommandCode.NUMS];
offCommands = new Command[CommandCode.NUMS];
for (int i = 0; i < CommandCode.NUMS; i++) {
onCommands[i] = new NoCommand();
offCommands[i] = new NoCommand();
}
}
public void setOnCommands(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();
}
}