设计模式--命令模式

设计模式---命令模式

命令模式可以将“动作的请求者”从“动作的执行者”对象中解耦。利用命令对象,把请求封装成一个特定对象。

 

 

Command:定义命令的接口,声明执行的方法。
ConcreteCommand:
命令接口实现对象,是“虚”的实现;通常会持有接收者,并调用接收者的功能来完成命令要执行的操作。
Receiver:
接收者,真正执行命令的对象。任何类都可能成为一个接收者,只要它能够实现命令要求实现的相应功能。
Invoker:
要求命令对象执行请求,通常会持有命令对象,可以持有很多的命令对象。这个是客户端真正触发命令并要求命令执行相应操作的地方,也就是说相当于使用命令对象的入口。
Client:创建具体的命令对象,并且设置命令对象的接收者。注意这个不是我们常规意义上的客户端,而是在组装命令对象和接收者,或许,把这个Client称为装配者会更好理解,因为真正使用命令的客户端是从Invoker来触发执行。


  1. /** 
  2.  *  
  3.  */  
  4. package com.model.command;  
  5.   
  6.   
  7. //命令接口  
  8. public interface Command {  
  9.   
  10.     public void execute();  
  11.       
  12.     public void undo();  
  13.       
  14. }  
  1. /** 
  2.  *  
  3.  */  
  4. package com.model.command;  
  5.   
  6.   
  7. //命令接口  
  8. public interface Command {  
  9.   
  10.     public void execute();  
  11.       
  12.     public void undo();  
  13.       
  14. }  


  1. /** 
  2.  *  
  3.  */  
  4. package com.model.command;  
  5.   
  6.   
  7.   
  8. //具体执行者  
  9. public class EditTextCommand implements Command {  
  10.       
  11.     private TxtObject txt;  
  12.       
  13.     public EditTextCommand(TxtObject txt){  
  14.         this.txt = txt;  
  15.     }  
  16.     /* (non-Javadoc) 
  17.      * @see com.model.command.Command#execute() 
  18.      */  
  19.     @Override  
  20.     public void execute() {  
  21.         txt.copy();  
  22.     }  
  23.   
  24.     /* (non-Javadoc) 
  25.      * @see com.model.command.Command#undo() 
  26.      */  
  27.     @Override  
  28.     public void undo() {  
  29.         txt.undo();  
  30.     }  
  31.   
  32. }  
  1. /** 
  2.  *  
  3.  */  
  4. package com.model.command;  
  5.   
  6.   
  7.   
  8. //具体执行者  
  9. public class EditTextCommand implements Command {  
  10.       
  11.     private TxtObject txt;  
  12.       
  13.     public EditTextCommand(TxtObject txt){  
  14.         this.txt = txt;  
  15.     }  
  16.     /* (non-Javadoc) 
  17.      * @see com.model.command.Command#execute() 
  18.      */  
  19.     @Override  
  20.     public void execute() {  
  21.         txt.copy();  
  22.     }  
  23.   
  24.     /* (non-Javadoc) 
  25.      * @see com.model.command.Command#undo() 
  26.      */  
  27.     @Override  
  28.     public void undo() {  
  29.         txt.undo();  
  30.     }  
  31.   
  32. }  

  1. /** 
  2.  *  
  3.  */  
  4. package com.model.command;  
  5.   
  6. //请求者  
  7. public class TxtObject {  
  8.   
  9.     public void copy(){  
  10.         System.out.println("This is copy function");  
  11.     }  
  12.       
  13.     public void undo(){  
  14.         System.out.println("This is undo function");  
  15.     }  
  16. }  
  1. /** 
  2.  *  
  3.  */  
  4. package com.model.command;  
  5.   
  6. //请求者  
  7. public class TxtObject {  
  8.   
  9.     public void copy(){  
  10.         System.out.println("This is copy function");  
  11.     }  
  12.       
  13.     public void undo(){  
  14.         System.out.println("This is undo function");  
  15.     }  
  16. }  

  1. /** 
  2.  *  
  3.  */  
  4. package com.model.command;  
  5.   
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. //命令接受者  
  10. public class CommandReceiver {  
  11.     //  
  12.     private List<Command> executeList;  
  13.       
  14.     public CommandReceiver(){  
  15.         executeList = new ArrayList<Command>();  
  16.     }  
  17.       
  18.     public void setExecuteList(Command command){  
  19.         executeList.add(command);  
  20.     }  
  21.       
  22.     public void copy(){  
  23.         executeList.get(executeList.size()-1).execute();  
  24.     }  
  25.       
  26.     public void undo(){  
  27.         executeList.get(executeList.size()-1).undo();  
  28.         executeList.remove(executeList.get(executeList.size()-1));  
  29.     }  
  30. }  
  1. /** 
  2.  *  
  3.  */  
  4. package com.model.command;  
  5.   
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. //命令接受者  
  10. public class CommandReceiver {  
  11.     //  
  12.     private List<Command> executeList;  
  13.       
  14.     public CommandReceiver(){  
  15.         executeList = new ArrayList<Command>();  
  16.     }  
  17.       
  18.     public void setExecuteList(Command command){  
  19.         executeList.add(command);  
  20.     }  
  21.       
  22.     public void copy(){  
  23.         executeList.get(executeList.size()-1).execute();  
  24.     }  
  25.       
  26.     public void undo(){  
  27.         executeList.get(executeList.size()-1).undo();  
  28.         executeList.remove(executeList.get(executeList.size()-1));  
  29.     }  
  30. }  

  1. package com.model.command;  
  2.   
  3. //发送请求客户端  
  4. public class Client {  
  5.       
  6.     public static void main(String[] args){  
  7.         CommandReceiver comm = new CommandReceiver();  
  8.         comm.setExecuteList(new EditTextCommand(new TxtObject()));  
  9.         comm.copy();  
  10.         comm.setExecuteList(new EditTextCommand(new TxtObject()));  
  11.         comm.copy();  
  12.         comm.undo();  
  13.         comm.undo();  
  14.     }  
  15. }  
  1. package com.model.command;  
  2.   
  3. //发送请求客户端  
  4. public class Client {  
  5.       
  6.     public static void main(String[] args){  
  7.         CommandReceiver comm = new CommandReceiver();  
  8.         comm.setExecuteList(new EditTextCommand(new TxtObject()));  
  9.         comm.copy();  
  10.         comm.setExecuteList(new EditTextCommand(new TxtObject()));  
  11.         comm.copy();  
  12.         comm.undo();  
  13.         comm.undo();  
  14.     }  
  15. }  


总结:命令模式是一个很优美的设计模式、但他和其他设计模式一样不完美、命令模式很好的将请求者和执行者进行了解耦、支持了事物的操作等一系列问题,解决了很多问题,在struts中被广泛利用,这一点也证明了他是非常成功的一个设计模式

posted on 2016-03-30 10:10  Leoxlu  阅读(148)  评论(0编辑  收藏  举报

导航