解析
- 别名: Action /Transaction
- 宏命令
- command与?模式
- 多次undo
- command与?模式
- trasaction回滚
- command与?模式
将命令的请求者(Controler)和命令的执行者(Device)进行解耦中间的就是命令(Command)
public class Main {
public static void main(String[] args) {
System.out.println("===客户端用【可编程式遥控器】操作电器===");
Device tv = new TV();
Device radio = new Radio();
Controller controller = new Controller();
//绑定【电视机】的【命令】到【控制器按键】
controller.bindOKCommand(new SwitchCommand(tv));
controller.bindVerticalCommand(new ChannelCommand(tv));//上下调台
controller.bindHorizontalCommand(new VolumeCommand(tv));//左右调音
controller.buttonOKHold();
controller.buttonUpClick();
controller.buttonUpClick();
controller.buttonDownClick();
controller.buttonRightClick();
/*打印输出:
===客户端用【可编程式遥控器】操作电器===
长按OK按键……电视机启动
单击↑按键……电视机频道+
单击↑按键……电视机频道+
单击↓按键……电视机频道-
单击→按键……电视机音量+
*/
}
}
interface Switchable {
void on();
void off();
}
interface Device extends Switchable {
// 频道+
public void channelUp();
// 频道-
public void channelDown();
// 音量+
public void volumeUp();
// 音量-
public void volumeDown();
}
class TV implements Device {
@Override
public void on() {
System.out.println("电视机启动");
}
@Override
public void off() {
System.out.println("电视机关闭");
}
@Override
public void channelUp() {
System.out.println("电视机频道+");
}
@Override
public void channelDown() {
System.out.println("电视机频道-");
}
@Override
public void volumeUp() {
System.out.println("电视机音量+");
}
@Override
public void volumeDown() {
System.out.println("电视机音量-");
}
}
interface Command {
// 执行命令操作
void exe();
// 反执行命令操作
void unexe();
}
class SwitchCommand implements Command {
private Device device;// 此处持有高级设备接口。
public SwitchCommand(Device device) {
this.device = device;
}
@Override
public void exe() {
device.on();// 执行命令调用开机操作
}
@Override
public void unexe() {
device.off();// 反执行命令调用关机操作
}
}
class ChannelCommand implements Command {
private Device device;
public ChannelCommand(Device device) {
this.device = device;
}
@Override
public void exe() {
device.channelUp();
}
@Override
public void unexe() {
device.channelDown();
}
}
class VolumeCommand implements Command {
private Device device;
public VolumeCommand(Device device) {
this.device = device;
}
@Override
public void exe() {
device.volumeUp();
}
@Override
public void unexe() {
device.volumeDown();
}
}
class Controller {
private Command okCommand;
private Command verticalCommand;
private Command horizontalCommand;
// 绑定OK键命令
public void bindOKCommand(Command okCommand) {
this.okCommand = okCommand;
}
// 绑定上下方向键命令
public void bindVerticalCommand(Command verticalCommand) {
this.verticalCommand = verticalCommand;
}
// 绑定左右方向键命令
public void bindHorizontalCommand(Command horizontalCommand) {
this.horizontalCommand = horizontalCommand;
}
// 开始按键映射命令
public void buttonOKHold() {
System.out.print("长按OK按键……");
okCommand.exe();
}
public void buttonOKClick() {
System.out.print("单击OK按键……");
okCommand.unexe();
}
public void buttonUpClick() {
System.out.print("单击↑按键……");
verticalCommand.exe();
}
public void buttonDownClick() {
System.out.print("单击↓按键……");
verticalCommand.unexe();
}
public void buttonLeftClick() {
System.out.print("单击←按键……");
horizontalCommand.unexe();
}
public void buttonRightClick() {
System.out.print("单击→按键……");
horizontalCommand.exe();
}