设计模式18---设计模式之策略模式(Strategy)(行为型)
1.场景模拟
简单的报价管理系统:
对于普通用户和新用户报全价
对于老客户统一折扣5%
对于大客户统一折扣10%
2.不用模式的解决方案
package demo16.strategy.example2; /** * 价格管理,主要完成计算向客户所报价格的功能 */ public class Price { /** * 报价,对不同类型的,计算不同的价格 * @param goodsPrice 商品销售原价 * @param customerType 客户类型 * @return 计算出来的,应该给客户报的价格 */ public double quote(double goodsPrice,String customerType){ if("普通客户".equals(customerType)){ return this.calcPriceForNormal(goodsPrice); }else if("老客户".equals(customerType)){ return this.calcPriceForOld(goodsPrice); }else if("大客户".equals(customerType)){ return this.calcPriceForLarge(goodsPrice); } //其余人员都是报原价 return goodsPrice; } /** * 为新客户或者是普通客户计算应报的价格 * @param goodsPrice 商品销售原价 * @return 计算出来的,应该给客户报的价格 */ private double calcPriceForNormal(double goodsPrice){ System.out.println("对于新客户或者是普通客户,没有折扣"); return goodsPrice; } /** * 为老客户计算应报的价格 * @param goodsPrice 商品销售原价 * @return 计算出来的,应该给客户报的价格 */ private double calcPriceForOld(double goodsPrice){ System.out.println("对于老客户,统一折扣5%"); return goodsPrice*(1-0.05); } /** * 为大客户计算应报的价格 * @param goodsPrice 商品销售原价 * @return 计算出来的,应该给客户报的价格 */ private double calcPriceForLarge(double goodsPrice){ System.out.println("对于大客户,统一折扣10%"); return goodsPrice*(1-0.1); } }
3.有何问题?
会经常有这样的需要,在公司周年庆的时候,所有的客户额外增加3%的折扣,在换季的时候普通用户额外增加3%的折扣,然后过了促销时间,价格又要涨回来,那么这个价格类将会非常的庞大,而且,方法非常的多。
看到这,朋友们很快就想到:如何实现才能让价格类中的计算报价的算法,很容易的实现可维护,可扩展,而且可以动态的切换变化呢?
4.使用策略模式来解决问题
4.1策略模式定义
定义一系列的算法,把他们一个个封装起来,并且可以使他们相互替换,本模式使得算法可独立于使用它的客户而变化。
4.2策略模式的结构图
4.3策略模式示例代码
package demo16.strategy.example3; /** * 策略,定义算法的接口 */ public interface Strategy { /** * 某个算法的接口,可以有传入参数,也可以有返回值 */ public void algorithmInterface(); } ********************************************************************* package demo16.strategy.example3; /** * 实现具体的算法 */ public class ConcreteStrategyA implements Strategy { public void algorithmInterface() { //具体的算法实现 } } ********************************************************************* package demo16.strategy.example3; /** * 实现具体的算法 */ public class ConcreteStrategyB implements Strategy { public void algorithmInterface() { //具体的算法实现 } } package demo16.strategy.example3; /** * 实现具体的算法 */ public class ConcreteStrategyC implements Strategy { public void algorithmInterface() { //具体的算法实现 } } ********************************************************************* package demo16.strategy.example3; /** * 上下文对象,通常会持有一个具体的策略对象 */ public class Context { /** * 持有一个具体的策略对象 */ private Strategy strategy; /** * 构造方法,传入一个具体的策略对象 * @param aStrategy 具体的策略对象 */ public Context(Strategy aStrategy) { this.strategy = aStrategy; } /** * 上下文对客户端提供的操作接口,可以有参数和返回值 */ public void contextInterface() { //通常会转调具体的策略对象进行算法运算 strategy.algorithmInterface(); } }
5.使用策略模式重写实例
package demo16.strategy.example4; /** * 策略,定义计算报价算法的接口 */ public interface Strategy { /** * 计算应报的价格 * @param goodsPrice 商品销售原价 * @return 计算出来的,应该给客户报的价格 */ public double calcPrice(double goodsPrice); } ************************************************************************ package demo16.strategy.example4; /** * 具体算法实现,为新客户或者是普通客户计算应报的价格 */ public class NormalCustomerStrategy implements Strategy{ public double calcPrice(double goodsPrice) { System.out.println("对于新客户或者是普通客户,没有折扣"); return goodsPrice; } } *********************************************************************** package demo16.strategy.example4; /** * 具体算法实现,为老客户计算应报的价格 */ public class OldCustomerStrategy implements Strategy{ public double calcPrice(double goodsPrice) { System.out.println("对于老客户,统一折扣5%"); return goodsPrice*(1-0.05); } } *********************************************************************** package demo16.strategy.example4; /** * 具体算法实现,为大客户计算应报的价格 */ public class LargeCustomerStrategy implements Strategy{ public double calcPrice(double goodsPrice) { System.out.println("对于大客户,统一折扣10%"); return goodsPrice*(1-0.1); } } *********************************************************************** package demo16.strategy.example4; /** * 价格管理,主要完成计算向客户所报价格的功能 */ public class Price { /** * 持有一个具体的策略对象 */ private Strategy strategy = null; /** * 构造方法,传入一个具体的策略对象 * @param aStrategy 具体的策略对象 */ public Price(Strategy aStrategy){ this.strategy = aStrategy; } /** * 报价,计算对客户的报价 * @param goodsPrice 商品销售原价 * @return 计算出来的,应该给客户报的价格 */ public double quote(double goodsPrice){ return this.strategy.calcPrice(goodsPrice); } } *********************************************************************** package demo16.strategy.example4; public class Client { public static void main(String[] args) { //1:选择并创建需要使用的策略对象 Strategy strategy = new LargeCustomerStrategy(); //2:创建上下文 Price ctx = new Price(strategy); //3:计算报价 double quote = ctx.quote(1000); System.out.println("向客户报价:"+quote); } }
6.模式讲解
6.1要点
功能:把具体的算法从具体的业务处理中独立出来,把他们实现成为单独的算法类,从而形成一系列的算法,并让这些算法可以相互替换。
策略算法是相同行为的不同实现
什么时候选用:多个if-else语句的时候就可以选用
6.2策略模式的调用顺序示意图
首先客户端选择并创建具体的策略对象
其次创建上下文
最后调用上下文的方法来执行功能了
6.3当添加新的策略时候
只需要动下面代码,是不是很简单,很方便呢?
//1:选择并创建需要使用的策略对象
Strategy strategy = new LargeCustomerStrategy();
6.4另一种策略模式调用示意图
6.5策略模式优缺点
优点:定义一系列算法,避免使用多重条件语句,更好的扩展性
缺点:客户端必须了解每种策略的不同,增加了对象数目,只适合扁平的算法结构(地位平等的算法)
6.6设计模式的本质
分离算法,选择实现