Template Method与Strategy模式区别

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值