命令模式(Command):
将请求封装成对象,以便使用不同的请求、日志、队列等来参数化其他对象。命令模式也支持撤销操作。
命令模式的角色:
1)传递命令对象(Invoker):是请求的发送者,它通常拥有很多的命令对象,并通过访问命令对象来执行相关请求,它不直接访问接收者。
2)抽象命令接口(Command):声明执行命令的接口,拥有执行命令的抽象方法execute()。
3)具体的命令对象(ConcreteCommand):是抽象命令类的具体实现类,它拥有接收者对象,并通过调用接收者的功能来完成命令要执行的操作。
4)接受者对象(Receiver):执行命令功能的相关操作,是具体命令对象业务的真正实现者。
5)客户端对象(Client):创建具体命令的对象并且设置命令对象的接受者。
示例:
土豪小明是一个狂热的科技迷,因此将家中电器进行了全面升级,更换成了智能家电,通过小爱同学可以统一控制,只需要一句"小爱同学,打开电灯"...,那么我们的故事就从这里开始。
1 internal class Program 2 { 3 private static void Main(string[] args) 4 { 5 // 创建电灯的对象 6 LightReceiver lightReceiver = new LightReceiver(); 7 // 创建电灯的开关命令 8 Command lightOnCommand = new LightOnCommand(lightReceiver); 9 Command lightOffCommand = new LightOffCommand(lightReceiver); 10 // 创建遥控器-小爱同学 11 RemoteController remoteController = new RemoteController(); 12 // 设置 13 remoteController.setCommand(0, lightOnCommand, lightOffCommand); 14 // 执行命令 15 remoteController.onExecute(0); 16 remoteController.Undo(); 17 } 18 } 19 20 /// <summary> 21 /// 抽象命令接口 22 /// </summary> 23 public interface Command 24 { 25 /// <summary> 26 /// 执行 27 /// </summary> 28 void execute(); 29 30 /// <summary> 31 /// 撤销 32 /// </summary> 33 void undo(); 34 } 35 36 /// <summary> 37 /// 接受者对象 38 /// </summary> 39 public class LightReceiver 40 { 41 public void on() 42 { 43 Console.WriteLine("电灯打开了..."); 44 } 45 46 public void off() 47 { 48 Console.WriteLine("电灯关闭了..."); 49 } 50 } 51 52 /// <summary> 53 /// 具体的命令对象 54 /// </summary> 55 internal class LightOnCommand : Command 56 { 57 private readonly LightReceiver lightReceiver; 58 59 public LightOnCommand(LightReceiver lightReceiver) 60 { 61 this.lightReceiver = lightReceiver; 62 } 63 64 public void execute() 65 { 66 // 调用接收者的方法 67 lightReceiver.on(); 68 } 69 70 public void undo() 71 { 72 // 调用接收者的方法 73 lightReceiver.off(); 74 } 75 } 76 77 /// <summary> 78 /// 具体的命令对象 79 /// </summary> 80 internal class LightOffCommand : Command 81 { 82 private readonly LightReceiver lightReceiver; 83 84 public LightOffCommand(LightReceiver lightReceiver) 85 { 86 this.lightReceiver = lightReceiver; 87 } 88 89 public void execute() 90 { 91 // 调用接收者的方法 92 lightReceiver.off(); 93 } 94 95 public void undo() 96 { 97 // 调用接收者的方法 98 lightReceiver.on(); 99 } 100 } 101 102 /// <summary> 103 /// 空命令,用于初始化 104 /// </summary> 105 internal class NoCommand : Command 106 { 107 public void execute() 108 { 109 } 110 111 public void undo() 112 { 113 } 114 } 115 116 internal class RemoteController 117 { 118 // 按钮的命令数组 119 private Command[] onCommands; 120 121 private Command[] offCommands; 122 123 // 撤销命令 124 private Command undoCommand; 125 126 public RemoteController() 127 { 128 // 假设小爱同学需要连接5款智能家电 129 onCommands = new Command[5]; 130 offCommands = new Command[5]; 131 for (int i = 0; i < 5; i++) 132 { 133 onCommands[i] = new NoCommand(); 134 offCommands[i] = new NoCommand(); 135 } 136 } 137 138 // 给小爱同学设置需要的命令 139 public void setCommand(int no, Command onCommand, Command offCommand) 140 { 141 onCommands[no] = onCommand; 142 offCommands[no