策略模式

来源:互联网 发布:四年级上册优化的答案 编辑:程序博客网 时间:2024/05/17 13:46

策略模式用于封装系列的算法,这些算法通常被封装在一个被称为Context的类呷,客户端程序可以自由选择其中一种算法,或让Context为客户端选择一个最佳的算法——使用策略模式的优势是为了支持算法的自由切换。

假设这样的场景:假如正在开发一个网上书店,该书店为了更好地促销,经常需要对图书进行打折促销,程序需要考虑各种打折促销的计算方法。下面是实现的UML类图。


下面先提供一个打折算法的接口,该接口里包含一个getDiscount()方法,该接口的代码如下。

 public interface DiscountStrategy{// 定义一个用于计算打折价的方法double getDiscount(double originPrice);}

下面为该打折接口提供两个策略类,它们分别实现了不同的打折算法。

// 实现DiscountStrategy接口,实现对VIP打折的算法public class VipDiscountimplements DiscountStrategy{// 重写getDiscount()方法,提供VIP打折算法public double getDiscount(double originPrice){System.out.println("使用VIP折扣...");return originPrice * 0.5;}} public class OldDiscountimplements DiscountStrategy{// 重写getDiscount()方法,提供旧书打折算法public double getDiscount(double originPrice){System.out.println("使用旧书折扣...");return originPrice * 0.7;}}
  提供了如上的两个打折策略类之后,程序还应该提供 一个DiscountContext类,该类用于为客户端代码选择折扣策略,当然也允许用户自由选择折扣。
 public class DiscountContext{// 组合一个DiscountStrategy对象private DiscountStrategy strategy;// 构造器,传入一个DiscountStrategy对象public DiscountContext(DiscountStrategy strategy){this.strategy  = strategy;}// 根据实际所使用的DiscountStrategy对象得到折扣价public double getDiscountPrice(double price){// 如果strategy为null,系统自动选择OldDiscount类if (strategy == null){strategy = new OldDiscount();}return this.strategy.getDiscount(price);}// 提供切换算法的方法public void changeDiscount(DiscountStrategy strategy){this.strategy = strategy;}}

下面的程序示范了使用该Context类来处理图书打折的任何情况。

public class StrategyTest{public static void main(String[] args){// 客户端没有选择打折策略类DiscountContext dc = new DiscountContext(null);double price1 = 79;// 使用默认的打折策略System.out.println("99元的书默认打折后的价格是:"+ dc.getDiscountPrice(price1));// 客户端选择合适的VIP打折策略dc.changeDiscount(new VipDiscount());double price2 = 89;// 使用VIP打折得到打折价格System.out.println("109元的书对VIP用户的价格是:"+ dc.getDiscountPrice(price2));}}

执结果。





0 0
原创粉丝点击