设计模式-策略模式、命令模式

 策略模式

所谓策略模式,就是将通用逻辑中的一部分逻辑抽象出来,这部分被抽象出来的逻辑会根据问题的不同而发生变化(具体实现不唯一)。

 1 interface IStrategy {  
 2     public void doSomething();  
 3 }  
 4 class Strategy1 implements IStrategy {  
 5     public void doSomething() {  
 6         System.out.println("具体策略1");  
 7     }  
 8 }  
 9 class Strategy2 implements IStrategy {  
10     public void doSomething() {  
11         System.out.println("具体策略2");  
12     }  
13 }  
14 class Context {  
15     private IStrategy strategy;  
16       
17     public Context(IStrategy strategy){  
18         this.strategy = strategy;  
19     }  
20       
21     public void execute(){  
22         strategy.doSomething();  
23     }  
24 }  
25   
26 public class Client {  
27     public static void main(String[] args){  
28         Context context;  
29         System.out.println("-----执行策略1-----");  
30         context = new Context(new Strategy1());  
31         context.execute();  
32   
33         System.out.println("-----执行策略2-----");  
34         context = new Context(new Strategy2());  
35         context.execute();  
36     }  
37 }  

命令模式

用于“行为请求者(命令发出者)”与“行为实现者(具体的命令)”解耦,可实现二者之间的松耦合,以便适应变化。

  1 //执行命令的接口  
  2 public interface Command {  
  3   void execute();  
  4 }  
  5 //命令接收者Receiver  
  6 public class TV {  
  7   public int currentChannel = 0;  
  8   
  9   public void turnOn() {  
 10      System.out.println("The televisino is on.");  
 11   }  
 12   
 13   public void turnOff() {  
 14      System.out.println("The television is off.");  
 15   }  
 16   
 17   public void changeChannel(int channel) {  
 18      this.currentChannel = channel;  
 19      System.out.println("Now TV channel is " + channel);  
 20   }  
 21 }  
 22 
 23 //开机命令ConcreteCommand  
 24 public class CommandOn implements Command {  
 25   private TV myTv;  
 26 
 27   public CommandOn(TV tv) {  
 28      myTv = tv;  
 29   }  
 30   
 31   public void execute() {  
 32      myTv.turnOn();  
 33   }  
 34 }  
 35 //关机命令ConcreteCommand  
 36 public class CommandOff implements Command {  
 37   private TV myTv;  
 38   
 39   public CommandOff(TV tv) {  
 40      myTv = tv;  
 41   }  
 42   
 43   public void execute() {  
 44      myTv.turnOff();  
 45   }  
 46 }  
 47 //频道切换命令ConcreteCommand  
 48 public class CommandChange implements Command {  
 49   private TV myTv;  
 50   
 51   private int channel;  
 52   
 53   public CommandChange(TV tv, int channel) {  
 54      myTv = tv;  
 55      this.channel = channel;  
 56   }  
 57   
 58   public void execute() {  
 59      myTv.changeChannel(channel);  
 60   }  
 61 }  
 62 //可以看作是遥控器Invoker  
 63 public class Control {  
 64   private Command onCommand;
 65       private Command offCommand;
 66       private Command changeChannel;  
 67   
 68   public Control(Command on, Command off, Command channel) {  
 69      onCommand = on;  
 70      offCommand = off;  
 71      changeChannel = channel;  
 72   }  
 73   
 74   public void turnOn() {  
 75      onCommand.execute();  
 76   }  
 77   
 78   public void turnOff() {  
 79      offCommand.execute();  
 80   }  
 81   
 82   public void changeChannel() {  
 83      changeChannel.execute();  
 84   }  
 85 }  
 86 //测试类Client  
 87 public class Client {  
 88   public static void main(String[] args) {  
 89      // 命令接收者Receiver  
 90      TV myTv = new TV();  
 91      // 开机命令ConcreteCommond  
 92      CommandOn on = new CommandOn(myTv);  
 93      // 关机命令ConcreteCommond  
 94      CommandOff off = new CommandOff(myTv);  
 95      // 频道切换命令ConcreteCommond  
 96      CommandChange channel = new CommandChange(myTv, 2);  
 97      // 命令控制对象Invoker  
 98      Control control = new Control(on, off, channel);  
 99   
100      // 开机  
101      control.turnOn();  
102      // 切换频道  
103      control.changeChannel();  
104      // 关机  
105      control.turnOff();  
106   }  
107 }  

如果将命令模式中的Receiver(TV)去掉就是简约命令模式(感觉Receiver完全可以去掉),实在看不出与策略模式的区别~

参考:http://blog.csdn.net/jason0539/article/category/3092021

原文地址:https://www.cnblogs.com/holoyong/p/7300811.html