Template Method模式和Strategy模式有何异同

来源:互联网 发布:博士后 国外知乎 编辑:程序博客网 时间:2024/05/29 19:35

Template Method模式和Strategy模式有何异同

    博客分类: 
  • 设计模式
Java 
Template Method模式很容易理解,就是由基类提供一个模板,将各子类中不变的行为提取到基类中实现,而各子类中可变的行为则由各子类自己重写基类方法实现. 
Strategy则是在使用策略模式的应用实例内部维护一个策略实例,针对不同的子类用不同的策略实现. 

来看看两者的代码实现: 

Template Method模式 -- 基类 
Java代码  收藏代码
  1. package com.dzeay.pattern.template_method;  
  2.   
  3. public class BaseTemplate {  
  4.     public void doSameThing() {  
  5.         System.out.println("BaseTemplate.doSameThing");  
  6.     }  
  7.   
  8.     public void doOtherThing() {  
  9.         System.out.println("BaseTemplate.BaseTemplate");  
  10.     }  
  11. }  


Template Method模式 -- 子类A 
Java代码  收藏代码
  1. package com.dzeay.pattern.template_method;  
  2.   
  3. public class AClass extends BaseTemplate {  
  4.     @Override  
  5.     public void doOtherThing() {  
  6.         System.out.println("AClassImpl.doOtherThing");  
  7.     }  
  8. }  


Template Method模式 -- 子类B 
Java代码  收藏代码
  1. package com.dzeay.pattern.template_method;  
  2.   
  3. public class BClass extends BaseTemplate {  
  4.     @Override  
  5.     public void doOtherThing() {  
  6.         System.out.println("BClassImpl.doOtherThing");  
  7.     }  
  8. }  


Template Method模式 -- 测试类 
Java代码  收藏代码
  1. package com.dzeay.pattern.template_method;  
  2.   
  3. /** 
  4.  * <pre> 
  5.  * Template Method(模板方法模式)详解:  
  6.  * 由基类提供一个模板,将各子类中不变的行为提取到基类中实现, 
  7.  * 而各子类中可变的行为由各子类自己重写基类方法实现 
  8.  * </pre> 
  9.  *  
  10.  * @author <a href="mailto:dzeay.com@gmail.com">dzeay.com</a> 
  11.  * @since 2010-11-15 
  12.  * @version 1.0 
  13.  */  
  14. public class TestClass {  
  15.     /** 
  16.      *  
  17.      * @param args 
  18.      */  
  19.     public static void main(String[] args) {  
  20.         BaseTemplate aClass = new AClass();  
  21.         aClass.doSameThing();  
  22.         aClass.doOtherThing();  
  23.   
  24.         BaseTemplate bClass = new BClass();  
  25.         bClass.doSameThing();  
  26.         bClass.doOtherThing();  
  27.     }  
  28. }  


Strategy模式 -- 策略接口 
Java代码  收藏代码
  1. package com.dzeay.pattern.strategy;  
  2.   
  3. public interface IStrategy {  
  4.     public void doOtherThing();  
  5. }  


Strategy模式 -- 策略A 
Java代码  收藏代码
  1. package com.dzeay.pattern.strategy;  
  2.   
  3. public class AStrategy implements IStrategy {  
  4.     @Override  
  5.     public void doOtherThing() {  
  6.         System.out.println("AStrategy.doOtherThing");  
  7.     }  
  8. }  


Strategy模式 -- 策略B 
Java代码  收藏代码
  1. package com.dzeay.pattern.strategy;  
  2.   
  3. public class BStrategy implements IStrategy {  
  4.     @Override  
  5.     public void doOtherThing() {  
  6.         System.out.println("BStrategy.doOtherThing");  
  7.     }  
  8. }  


Strategy模式 -- 应用 
Java代码  收藏代码
  1. package com.dzeay.pattern.strategy;  
  2.   
  3. public class Context {  
  4.     private IStrategy strategy;  
  5.   
  6.     public Context() {  
  7.   
  8.     }  
  9.   
  10.     public Context(IStrategy strategy) {  
  11.         this.strategy = strategy;  
  12.     }  
  13.   
  14.     public void doOtherThing() {  
  15.         this.strategy.doOtherThing();  
  16.     }  
  17.   
  18.     public void setStrategy(IStrategy strategy) {  
  19.         this.strategy = strategy;  
  20.     }  
  21. }  


Strategy模式 -- 测试类 
Java代码  收藏代码
  1. package com.dzeay.pattern.strategy;  
  2.   
  3. /** 
  4.  * <pre> 
  5.  * Strategy(策略模式)详解: 
  6.  * 在使用策略模式的应用实例内部维护一个strategy实例,针对不同的子类用不同的策略实现 
  7.  * </pre> 
  8.  *  
  9.  * @author <a href="mailto:dzeay.com@gmail.com">dzeay.com</a> 
  10.  * @since 2010-11-15 
  11.  * @version 1.0 
  12.  */  
  13. public class TestClass {  
  14.     /** 
  15.      * @param args 
  16.      */  
  17.     public static void main(String[] args) {  
  18.         Context context = new Context();  
  19.   
  20.         context.setStrategy(new AStrategy());  
  21.         context.doOtherThing();  
  22.   
  23.         context.setStrategy(new BStrategy());  
  24.         context.doOtherThing();  
  25.           
  26.         context.setStrategy(new IStrategy() {              
  27.             @Override  
  28.             public void doOtherThing() {  
  29.                 System.out.println("doOtherThing");  
  30.             }  
  31.         });  
  32.         context.doOtherThing();  
  33.     }  
  34. }  


未完待续 ........
0 0
原创粉丝点击