Java设计模式——Command模式(容易,次要)

转自:http://www.cnblogs.com/shamgod/p/4593772.html

一、概述

命令模式:客户端对服务器端发出各种命令

 

二、代码

 

1.Client.java

复制代码
1 public class Client {
2     
3     public void request(Server server){
4         server.addCommand(new TextCommand());
5         server.addCommand(new ImageCommand());
6         server.doSomething();
7     }
8 }
复制代码

 

2.Server.java

复制代码
 1 public class Server {
 2 
 3     private List<Command> commands = new ArrayList<Command>();
 4 
 5     public void doSomething() {
 6         for(Command c : commands){
 7             c.execute();
 8         }
 9     }
10 
11     public void addCommand(Command command) {
12         commands.add(command);
13     }
14 
15 }
复制代码

 

3.Command.java

复制代码
1 public abstract class Command {
2     
3     public abstract void execute();
4     public abstract void unDo();
5 
6 }
复制代码

 

4.TextCommand.java

复制代码
 1 public class TextCommand extends Command {
 2 
 3     @Override
 4     public void execute() {
 5         System.out.println("TextCommand...........");
 6     }
 7 
 8     @Override
 9     public void unDo() {
10         // 涉及到操作的历史记录
11     }
12 
13 }
复制代码

 

5.ImageCommand.java

复制代码
 1 public class ImageCommand extends Command {
 2 
 3     @Override
 4     public void execute() {
 5         System.out.println("ImageCommand...........");
 6     }
 7 
 8     @Override
 9     public void unDo() {
10         // 涉及到操作的历史记录
11     }
12 
13 }
复制代码

 

6.Test.java

复制代码
1 public class Test {
2     
3     @org.junit.Test
4     public void test(){
5         Client c = new Client();
6         c.request(new Server());
7     }
8 
9 }
复制代码

 

三、运行结果

转自:http://men4661273.iteye.com/blog/1633775

 Command定义 
    将一个请求封装为一个对象,从而可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可取消的操作。 
     优点: 解耦了调用者和接受者之间联系。调用者调用一个操作,接受者接受请求执行相应的动作,因为使用Command模式解耦,调用者无需知道接受者任何接口。 
     缺点: 造成出现过多的具体命令类 

代码例子如下: 

播放器执行命令者: 

Java代码  收藏代码
  1. package command;  
  2.   
  3. //命令执行者播放器  
  4. public class Player {  
  5.   
  6.     public void turnOn(){  
  7.           
  8.         System.out.println("打开");  
  9.     }  
  10.       
  11.     public void turnOff(){  
  12.         System.out.println("关闭");  
  13.           
  14.     }  
  15.       
  16.     public void next(){  
  17.           
  18.         System.out.println("下一曲");  
  19.     }  
  20. }  
    

命令接口: 
Java代码  收藏代码
  1. package command;  
  2.   
  3. public interface Command {  
  4.   
  5.     public void execute();  
  6. }  


各种命令实现类: 
Java代码  收藏代码
  1. package command;  
  2. //打开命令类  
  3. public class TurnOnCommand implements Command {  
  4.   
  5.     private Player player;  
  6.       
  7.     public TurnOnCommand(Player player) {  
  8.         this.player = player;  
  9.     }  
  10.       
  11.     public void execute() {  
  12.         this.player.turnOn();  
  13.     }  
  14.   
  15. }  
  16.   
  17.   
  18. package command;  
  19. //关闭命令类  
  20. public class TurnOffCommand implements Command {  
  21.   
  22. private Player player;  
  23.       
  24.     public TurnOffCommand(Player player) {  
  25.         this.player = player;  
  26.     }  
  27.       
  28.     public void execute() {  
  29.         this.player.turnOff();  
  30.     }  
  31.   
  32. }  
  33.   
  34.   
  35. package command;  
  36. //下一曲命令类  
  37. public class NextCommand implements Command {  
  38.   
  39. private Player player;  
  40.       
  41.     public NextCommand(Player player) {  
  42.         this.player = player;  
  43.     }  
  44.       
  45.     public void execute() {  
  46.         this.player.next();  
  47.     }  
  48.   
  49. }  


命令发送者类: 
Java代码  收藏代码
  1. package command;  
  2. //命令发送类  
  3. public class PlayerInvoker {  
  4.   
  5.     private TurnOnCommand on;  
  6.     private TurnOffCommand off;  
  7.     private NextCommand next;  
  8.     public PlayerInvoker(TurnOnCommand on,TurnOffCommand off,NextCommand next) {  
  9.         this.on = on;  
  10.         this.off = off;  
  11.         this.next = next;  
  12.     }  
  13.       
  14.     public void turnOn(){         
  15.         this.on.execute();  
  16.     }  
  17.       
  18.     public void turnOff(){        
  19.         this.off.execute();  
  20.     }  
  21.       
  22.     public void next(){       
  23.         this.next.execute();  
  24.     }  
  25. }  


测试类: 
Java代码  收藏代码
  1. package command;  
  2. //测试类  
  3. public class Text {  
  4.       
  5. public static void main(String[] args) {  
  6.       
  7.     Player player = new Player();  
  8.     TurnOnCommand  on = new TurnOnCommand(player);  
  9.     TurnOffCommand  off = new TurnOffCommand(player);  
  10.     NextCommand  next = new NextCommand(player);  
  11.       
  12.     PlayerInvoker invoker = new PlayerInvoker(on, off, next);  
  13.       
  14.     invoker.turnOn();  
  15.     invoker.turnOff();  
  16.     invoker.next();  
  17. }  
  18. }  


    显然这样做的好处是符合封装的特性,降低耦合度,Command 是将对行为进行封装的典型模式,这样做有利于代码的健壮性 可维护性 还有复用性. 
  Command模式通常可应用到以下场景: 
  1 Multi-level undo(多级undo操作) 
    如果系统需要实现多级回退操作,这时如果所有用户的操作都以command对象的形式实现,系统可以简 
    单地用stack来保存最近执行的命令,如果用户需要执行undo操作,系统只需简单地popup一个最近的 
    command对象然后执行它的undo()方法既可。 

  2 Transactional behavior(原子事务行为) 
    借助command模式,可以简单地实现一个具有原子事务的行为。当一个事务失败时,往往需要回退到执 
    行前的状态,可以借助command对象保存这种状态,简单地处理回退操作。 

  3 Progress bars(状态条) 
    假如系统需要按顺序执行一系列的命令操作,如果每个command对象都提供一个 
    getEstimatedDuration()方法,那么系统可以简单地评估执行状态并显示出合适的状态条。 

  4 Wizards(导航) 
    通常一个使用多个wizard页面来共同完成一个简单动作。一个自然的方法是使用一个command对象来封 
    装wizard过程,该command对象在第一个wizard页面显示时被创建,每个wizard页面接收用户输入并设 
    置到该command对象中,当最后一个wizard页面用户按下“Finish”按钮时,可以简单地触发一个事件 
    调用execute()方法执行整个动作。通过这种方法,command类不包含任何跟用户界面有关的代码,可以 
    分离用户界面与具体的处理逻辑。 

  5 GUI buttons and menu items(GUI按钮与菜单条等等) 
    Swing系统里,用户可以通过工具条按钮,菜单按钮执行命令,可以用command对象来封装命令的执行。 

  6 Thread pools(线程池) 
    通常一个典型的线程池实现类可能有一个名为addTask()的public方法,用来添加一项工作任务到任务 
    队列中。该任务队列中的所有任务可以用command对象来封装,通常这些command对象会实现一个通用的 
    接口比如java.lang.Runnable。 

  7 Macro recording(宏纪录) 
    可以用command对象来封装用户的一个操作,这样系统可以简单通过队列保存一系列的command对象的状 
    态就可以记录用户的连续操作。这样通过执行队列中的command对象,就可以完成"Play back"操作了。 

  8 Networking 
    通过网络发送command命令到其他机器上运行。 

  9 Parallel Processing(并发处理) 
    当一个调用共享某个资源并被多个线程并发处理时。 



 



发布了71 篇原创文章 · 获赞 208 · 访问量 81万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章