设计模式系列之四_策略模式 和 模版方法模式(Template method)

来源:互联网 发布:js正则验证身份证号 编辑:程序博客网 时间:2024/05/18 09:49

1.策略模式

1.1 策略模式

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

1.2 策略模式的组成

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

1.3 使用场景  

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

/** * @Title:商品抽象折扣类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl  2013-3-12 下午03:24:53 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0  */public interface MemberStrategy {public double discountPrice(double bookPrice);}

/** * @Title:初级会员--折扣类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl  2013-3-12 下午03:53:59 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0  */public class JuniorMemberStrategy implements MemberStrategy{public double discountPrice(double bookPrice) {return bookPrice;}}

/** * @Title:中级会员--折扣类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl  2013-3-12 下午03:58:01 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0  */public class InterMediateMemberStrategy implements MemberStrategy{@Overridepublic double discountPrice(double bookPrice) {return bookPrice*0.9;}}

/** * @Title:高级会员--折扣类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl  2013-3-12 下午03:59:30 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0  */public class AdvancedMemberStrategy implements MemberStrategy{@Overridepublic double discountPrice(double bookPrice) {return bookPrice*0.8;}}

/** * @Title:图书价格类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl  2013-3-12 下午04:15:57 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0  */public class BookPrice {private MemberStrategy strategy;private double price;/** * @param strategy */public BookPrice(MemberStrategy strategy,double price) {super();this.strategy = strategy;this.price=price;}/** *  * getRealBookPrice(获取折扣后的价格) * @return double * @createDate 2013-3-12 下午04:30:26 * @since  1.0.0 */public double getRealBookPrice() {return strategy.discountPrice(this.price);}public MemberStrategy getStrategy() {return strategy;}public void setStrategy(MemberStrategy strategy) {this.strategy = strategy;}}

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

2 模版方法模式

2.1 模版方法模式

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


2.2 应用场景

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

实现代码如下:

/** * @Title:模版方法抽象类--定义一个业务的骨架,而将一些实现步骤延迟到子类中 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl  2013-3-12 下午02:10:38 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0  */public abstract class AbstractTemplate {/** *  * executeMain(需要在子类实现)  * @createDate 2013-3-12 下午02:12:43 * @since  1.0.0 */public abstract void executeMain();public void execute() {beforeAction();executeMain();afterAction();}public void beforeAction() {System.out.println("before Action...");}public void afterAction() {System.out.println("after Action...");}}

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

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

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


原创粉丝点击