一,命令模式的实现: 命令模式里边一般都有以下几个角色:客户端,请求者,命令接口,命令实现,接受者, 下边是简单命令模式的实现代码实现: 1public class Client{
2 public static void main(String[] args){ 3 Receiver receiver = new Receiver(); 4 Command commandOne = new ConcreteCommandOne(receiver); 5 Command commandTwo = new ConcreteCommandTwo(receiver); 6 Invoker invoker = new Invoker(commandOne,commandTwo); 7 invoker.actionOne(); 8 invoker.actionTwo(); 9 } 10} 11public class Invoker{ 12 private Command commandOne; 13 private Command commandTwo; 14 public Invoker(Command commandOne,Command commandTwo){ 15 this.commandOne = commandOne; 16 this.commandTwo = commandTwo; 17 } 18 public void actionOne(){ 19 commandOne.execute(); 20 } 21 public void actionTwo(){ 22 commandTwo.execute(); 23 } 24} 25public interface Command{ 26 void execute(); 27} 28public class ConcreteCommandOne implements Command{ 29 private Receiver receiver 30 public ConcreteCommandOne(Receiver receiver){ 31 this.receiver = receiver; 32 } 33 public void execute(){ 34 receiver.actionOne(); 35 } 36} 37public class ConcreteCommandTwo implements Command{ 38 private Receiver receiver 39 public ConcreteCommandTwo(Receiver receiver){ 40 this.receiver = receiver; 41 } 42 public void execute(){ 43 receiver.actionTwo(); 44 } 45} 46public class Receiver{ 47 public Receiver(){ 48 // 49 } 50 public void actionOne(){ 51 System.out.println("ActionOne has been taken."); 52 } 53 public void actionTwo(){ 54 System.out.println("ActionTwo has been taken."); 55 } 56} 二,命令模式的功能,好处,或者说为什么使用命令模式? 上边的代码是否看起来很傻呢,本来可以这样简单实现的: 1public class Client{
2 public static void main(String[] args){ 3 Receiver receiver = new Receiver(); 4 receiver.actionOne(); 5 receiver.actionTwo(); 6 } 7} 8public class Receiver{ 9 public Receiver(){ 10 // 11 } 12 public void actionOne(){ 13 System.out.println("ActionOne has been taken."); 14 } 15 public void actionTwo(){ 16 System.out.println("ActionTwo has been taken."); 17 } 18}
如下加入Log和排序后的Invoker 1public class Invoker{
2 private List cmdList = new ArrayList(); 3 public Invoker(){ 4 } 5 public add(Command command){ 6 cmdList.add(command); 7 } 8 public remove(Command command){ 9 cmdList.remove(command); 10 } 11 public void action(){ 12 Command cmd; 13 while((cmd =getCmd()) != null){ 14 log("begin"+cmd.getName()); 15 cmd.execute(); 16 log("end"+cmd.getName()); 17 } 18 } 19 public Command getCmd(){ 20 //按照自定义优先级,排序取出cmd 21 } 22} 23public class Client{ 24 public static void main(String[] args){ 25 Receiver receiver = new Receiver(); 26 Command commandOne = new ConcreteCommandOne(receiver); 27 Command commandTwo = new ConcreteCommandTwo(receiver); 28 Invoker invoker = new Invoker(); 29 invoker.add(commandOne); 30 invoker.add(commandTwo); 31 iinvoker.action(); 32 } 33}
2,需求:有个固定命令组合会多次被执行 1public class MacroCommand implements Command{
2 private List cmdList = new ArrayList(); 3 public add(Command command){ 4 cmdList.add(command); 5 } 6 public remove(Command command){ 7 cmdList.remove(command); 8 } 9 public void execute(){ 10 Command cmd; 11 for(int i=0;i<cmdList.size();i++){ 12 cmd = (Command)cmdList.get(i); 13 cmd.execute(); 14 } 15 } 16} 3,需求:须要redo undo 解决:加入备忘录模式,一个简单的实现如下 1public class ConcreteCommandOne implements Command{
2 private Receiver receiver 3 private Receiver lastReceiver; 4 public ConcreteCommandOne(Receiver receiver){ 5 this.receiver = receiver; 6 } 7 public void execute(){ 8 record(); 9 receiver.actionOne(); 10 } 11 public void undo(){ 12 //恢复状态 13 } 14 public void redo(){ 15 lastReceiver.actionOne(); 16 // 17 } 18 public record(){ 19 //记录状态 20 } 21} 4,需求:命令很多类似的地方 解决:使用原型模式,利用clone 这个就不写例子了。 四,命令模式的使用场合 1,须要callback的时候,例如java awt/swing/swt中的Listening的消息方式 2,须要对请求排队执行,命令的发送者和接受者有不同对的生命周期,就是命令执行的时候,可能发出命令的 Client已经不存在了 3,须要Redo Undo等函数 4,须要log每条命令 5,须要支持transaction,封装一组数据命令的时候. 五,最后再次总结一下命令模式的优点和缺点: 优点: 降低Client和命令接受者的耦合,是命令请求和命令执行的对象分割 便于修改和扩张 便于聚合多个命令 缺点: 造成出现过多的具体命令类,太多文件。 五,一个比较有意思的例子,来说明命令模式 Client :看电视的人 Invoker :遥控器 Command :电信号 具体命令 :遥控器上的按键对应的不同的电信号 Receiver :电视机 最后说一句,并不是全部按照模式写一定就好,应该根据你的需求来应用,或者全部应用,或者部分应用,或者根本不用。 |
|