模板模式和策略模式的区别【转】

来源:互联网 发布:C语言的算术表达式 编辑:程序博客网 时间:2024/05/16 01:43

1.策略模式

1.1 策略模式

  策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。
策略模式让算法独立于使用它的客户而独立变化。
  策略模式属于对象的行为模式。其用意是针对一组算法,将每一个算法封装到具有
共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到
客户端的情况下发生变化。  

1.2 策略模式的组成

  (1)抽象策略角色: 策略类,通常由一个接口或者抽象类实现。
  (2)具体策略角色:包装了相关的算法和行为。
类图如下:
  

1.3 使用场景  

  假设:现在要设计一个贩卖各类书籍的电子商务网站的购物车系统。
  一个最简单的情况就是把所有货品的单价乘上数量,但是实际情况肯定比这要复杂。
比如,本网站可能对所有的高级会员提供每本20%的促销折扣;对中级会员提供每本
10%的促销折扣;对初级会员没有折扣。
  根据描述,折扣是根据以下的几个算法中的一个进行的:
 算法一:对初级会员没有折扣。
 算法二:对中级会员提供10%的促销折扣。
 算法三:对高级会员提供20%的促销折扣。
实现代码如下:

[java] view plaincopy
  1. /** 
  2.  * @Title:商品抽象折扣类 
  3.  * @Description:TODO 
  4.  * @Company: Orclight's Studio 
  5.  * @author: shuzl  2013-3-12 下午03:24:53 
  6.  * @motto: Never put off what you can do today until Tomorrow 
  7.  * @version 1.0.0  
  8.  */  
  9. public interface MemberStrategy {  
  10.     public double discountPrice(double bookPrice);  
  11. }  

[java] view plaincopy
  1. /** 
  2.  * @Title:初级会员--折扣类 
  3.  * @Description:TODO 
  4.  * @Company: Orclight's Studio 
  5.  * @author: shuzl  2013-3-12 下午03:53:59 
  6.  * @motto: Never put off what you can do today until Tomorrow 
  7.  * @version 1.0.0  
  8.  */  
  9. public class JuniorMemberStrategy implements MemberStrategy{  
  10.     public double discountPrice(double bookPrice) {  
  11.         return bookPrice;  
  12.     }  
  13. }  

[java] view plaincopy
  1. /** 
  2.  * @Title:中级会员--折扣类 
  3.  * @Description:TODO 
  4.  * @Company: Orclight's Studio 
  5.  * @author: shuzl  2013-3-12 下午03:58:01 
  6.  * @motto: Never put off what you can do today until Tomorrow 
  7.  * @version 1.0.0  
  8.  */  
  9. public class InterMediateMemberStrategy implements MemberStrategy{  
  10.     @Override  
  11.     public double discountPrice(double bookPrice) {  
  12.         return bookPrice*0.9;  
  13.     }  
  14. }  

[java] view plaincopy
  1. /** 
  2.  * @Title:高级会员--折扣类 
  3.  * @Description:TODO 
  4.  * @Company: Orclight's Studio 
  5.  * @author: shuzl  2013-3-12 下午03:59:30 
  6.  * @motto: Never put off what you can do today until Tomorrow 
  7.  * @version 1.0.0  
  8.  */  
  9. public class AdvancedMemberStrategy implements MemberStrategy{  
  10.     @Override  
  11.     public double discountPrice(double bookPrice) {  
  12.         return bookPrice*0.8;  
  13.     }  
  14. }  

[java] view plaincopy
  1. /** 
  2.  * @Title:图书价格类 
  3.  * @Description:TODO 
  4.  * @Company: Orclight's Studio 
  5.  * @author: shuzl  2013-3-12 下午04:15:57 
  6.  * @motto: Never put off what you can do today until Tomorrow 
  7.  * @version 1.0.0  
  8.  */  
  9. public class BookPrice {  
  10.     private MemberStrategy strategy;  
  11.     private double price;  
  12.     /** 
  13.      * @param strategy 
  14.      */  
  15.     public BookPrice(MemberStrategy strategy,double price) {  
  16.         super();  
  17.         this.strategy = strategy;  
  18.         this.price=price;  
  19.     }  
  20.     /** 
  21.      *  
  22.      * getRealBookPrice(获取折扣后的价格) 
  23.      * @return double 
  24.      * @createDate 2013-3-12 下午04:30:26 
  25.      * @since  1.0.0 
  26.      */  
  27.     public double getRealBookPrice() {  
  28.         return strategy.discountPrice(this.price);  
  29.     }  
  30.       
  31.     public MemberStrategy getStrategy() {  
  32.         return strategy;  
  33.     }  
  34.     public void setStrategy(MemberStrategy strategy) {  
  35.         this.strategy = strategy;  
  36.     }  
  37. }  

[java] view plaincopy
  1. /** 
  2.  * @Title:图书打折后的价格--测试类 
  3.  * @Description:TODO 
  4.  * @Company: Orclight's Studio 
  5.  * @author: shuzl  2013-3-12 下午04:17:23 
  6.  * @motto: Never put off what you can do today until Tomorrow 
  7.  * @version 1.0.0  
  8.  */  
  9. public class TestBookPrice {  
  10.     public static void main(String[] args) {  
  11.         MemberStrategy strategy=new AdvancedMemberStrategy();  
  12.         BookPrice bookPrice = new BookPrice(strategy,99.0);  
  13.         System.out.println("图书价格是:"+bookPrice.getRealBookPrice());  
  14.     }  
  15. }  

2 模版方法模式

2.1 模版方法模式

  定义一个算法的骨架,而将一些实现步骤延迟到子类中。把不变的行为搬到超类,
去除子类中重复的代码来体现他的优势。
类图如下:


2.2 应用场景

  (1)一次性实现一个算法的不变的部分,并将可变的行为留给子类来实现
  (2)各子类中公共的行为应被提取出来并集中到一个公共父类中以避免代码重复。
首先识别现有代码中的不同之处,并且将不同之处分离为新的操作。最后,用一个
调用这些新的操作的模板方法来替换这些不同的代码。

实现代码如下:

[java] view plaincopy
  1. /** 
  2.  * @Title:模版方法抽象类--定义一个业务的骨架,而将一些实现步骤延迟到子类中 
  3.  * @Description:TODO 
  4.  * @Company: Orclight's Studio 
  5.  * @author: shuzl  2013-3-12 下午02:10:38 
  6.  * @motto: Never put off what you can do today until Tomorrow 
  7.  * @version 1.0.0  
  8.  */  
  9. public abstract class AbstractTemplate {  
  10.     /** 
  11.      *  
  12.      * executeMain(需要在子类实现)  
  13.      * @createDate 2013-3-12 下午02:12:43 
  14.      * @since  1.0.0 
  15.      */  
  16.     public abstract void executeMain();  
  17.       
  18.     public void execute() {  
  19.         beforeAction();  
  20.         executeMain();  
  21.         afterAction();  
  22.     }  
  23.       
  24.     public void beforeAction() {  
  25.         System.out.println("before Action...");  
  26.     }  
  27.       
  28.     public void afterAction() {  
  29.         System.out.println("after Action...");  
  30.     }  
  31. }  

[java] view plaincopy
  1. /** 
  2.  * @Title:模版方法子类--实现其中可变的部分 
  3.  * @Description:TODO 
  4.  * @Company: Orclight's Studio 
  5.  * @author: shuzl  2013-3-12 下午02:13:09 
  6.  * @motto: Never put off what you can do today until Tomorrow 
  7.  * @version 1.0.0  
  8.  */  
  9. public class TemplateImpl extends AbstractTemplate{  
  10.     public void executeMain() {  
  11.         System.out.println("核心操作处理...实现方式一");  
  12.     }  
  13. }  

[java] view plaincopy
  1. /** 
  2.  * @Title:模版方法测试类 
  3.  * @Description:TODO 
  4.  * @Company: Orclight's Studio 
  5.  * @author: shuzl  2013-3-12 下午02:14:18 
  6.  * @motto: Never put off what you can do today until Tomorrow 
  7.  * @version 1.0.0  
  8.  */  
  9. public class TestTemplate {  
  10.     public static void main(String[] args) {  
  11.         AbstractTemplate temp = new TemplateImpl();  
  12.         temp.execute();  
  13.     }  
  14. }  

参考文章:
1.策略模式_百度百科  
2.《JAVA与模式》26天系列—第16天—策略模式  
3.Java之模板方法模式  
0 0
原创粉丝点击