使用场景
根据用户的需求处理数据时候需要对算法做出选择,固定的一些算法(一般不再发生变化的),扩展比如在商场促销的时候往往会根据客户不同的等级设置不同的折扣等。
level1:各种if-else
package com.slp; import java.math.BigDecimal; public class QuoteManager { public BigDecimal quote(BigDecimal originalPrice,String customType){ if ("新客户".equals(customType)) { System.out.println("抱歉!新客户没有折扣!"); return originalPrice; }else if ("老客户".equals(customType)) { System.out.println("恭喜你!老客户打9折!"); originalPrice = originalPrice.multiply(new BigDecimal(0.9)).setScale(2,BigDecimal.ROUND_HALF_UP); return originalPrice; }else if("VIP客户".equals(customType)){ System.out.println("恭喜你!VIP客户打8折!"); originalPrice = originalPrice.multiply(new BigDecimal(0.8)).setScale(2,BigDecimal.ROUND_HALF_UP); return originalPrice; } //其他人员都是原价 return originalPrice; } }
可以正常使用但是也有不足:不同客户使用的报价的算法都放在一个方法里面,使得方法很庞大(吐槽一下,公司之前的方法就是这样的加上各种具体业务实现,真是看着头大)。
level2:方法单独封装
package com.slp; import java.math.BigDecimal; public class QuoteManagerImprove { public BigDecimal quote(BigDecimal originalPrice, String customType){ if ("新客户".equals(customType)) { return this.quoteNewCustomer(originalPrice); }else if ("老客户".equals(customType)) { return this.quoteOldCustomer(originalPrice); }else if("VIP客户".equals(customType)){ return this.quoteVIPCustomer(originalPrice); } //其他人员都是原价 return originalPrice; } /** * 对VIP客户的报价算法 * @param originalPrice 原价 * @return 折后价 */ private BigDecimal quoteVIPCustomer(BigDecimal originalPrice) { System.out.println("恭喜!VIP客户打8折"); originalPrice = originalPrice.multiply(new BigDecimal(0.8)).setScale(2,BigDecimal.ROUND_HALF_UP); return originalPrice; } /** * 对老客户的报价算法 * @param originalPrice 原价 * @return 折后价 */ private BigDecimal quoteOldCustomer(BigDecimal originalPrice) { System.out.println("恭喜!老客户打9折"); originalPrice = originalPrice.multiply(new BigDecimal(0.9)).setScale(2,BigDecimal.ROUND_HALF_UP); return originalPrice; } /** * 对新客户的报价算法 * @param originalPrice 原价 * @return 折后价 */ private BigDecimal quoteNewCustomer(BigDecimal originalPrice) { System.out.println("抱歉!新客户没有折扣!"); return originalPrice; } }
这个虽然会好一点,但是放新增折扣的时候,首先要添加方法,然后再添加一个else if分支,这违反了开闭原则:
开闭原则:对于扩展是开放的,这意味着模块的行为是可以扩展的,当应用的需求改变时,我们是可以对模块进行扩展,使其具有满足那些改变的新行为,也就是说,我们可以改变模块的功能。
对修改是关闭的,对模块进行扩展时,不必改动模块的源代码或者二进制代码。
而且如果不同时区折扣有变化还要全部进行修改,这个就会很繁琐。
WHAT:
策略模式定义了一系列算法,并将每一个算法封装起来,使每个算法可以相互替代,使算法本身和使用算法的客户使用端分离开,相互独立。
HOW:
1)策略接口角色IStrategy:用来约束一系列具体的算法策略,策略上下文角色ConcreteStrategy使用此接口来调用具体的策略所实现的算法
2)具体策略实现角色ConcreteStrategy:具体的策略实现,即具体的算法实现
3)策略上下文角色StrategyContext:策略上下文,负责和具体的策略实现交互,通常策略上下文对象会持有一个真正的策略实现对象,策略上下文还可以让具体的策略实现从其中获取相关数据,回调策略上下文对象的方法。
UML类图:
UML序列图:
一般实现方式:
package com.slp; //策略接口 public interface IStrategy { //定义的抽象算法方法 来约束具体的算法实现方法 public void algorithmMethod(); }
package com.slp; // 具体的策略实现 public class ConcreteStrategy implements IStrategy { //具体的算法实现 @Override public void algorithmMethod() { System.out.println("this is ConcreteStrategy method..."); } }
package com.slp; // 具体的策略实现2 public class ConcreteStrategy2 implements IStrategy { //具体的算法实现 @Override public void algorithmMethod() { System.out.println("this is ConcreteStrategy2 method..."); } }
package com.slp; /** * 策略上下文 */ public class StrategyContext { //持有一个策略实现的引用 private IStrategy strategy; //使用构造器注入具体的策略类 public StrategyContext(IStrategy strategy) { this.strategy = strategy; } public void contextMethod(){ //调用策略实现的方法 strategy.algorithmMethod(); } }
package com.slp; //外部客户端 public class Client { public static void main(String[] args) { //1.创建具体测策略实现 IStrategy strategy = new ConcreteStrategy2(); //2.在创建策略上下文的同时,将具体的策略实现对象注入到策略上下文当中 StrategyContext ctx = new StrategyContext(strategy); //3.调用上下文对象的方法来完成对具体策略实现的回调 ctx.contextMethod(); } }
针对最开始的例子可以使用这个模板进行改造:
package com.slp; import java.math.BigDecimal; //报价策略接口 public interface IQuoteStrategy { //获取折后价的价格 BigDecimal getPrice(BigDecimal originalPrice); }
package com.slp; import java.math.BigDecimal; //新客户的报价策略实现类 public class NewCustomerQuoteStrategy implements IQuoteStrategy { @Override public BigDecimal getPrice(BigDecimal originalPrice) { System.out.println("抱歉!新客户没有折扣!"); return originalPrice; } }
package com.slp; import java.math.BigDecimal; //老客户的报价策略实现 public class OldCustomerQuoteStrategy implements IQuoteStrategy { @Override public BigDecimal getPrice(BigDecimal originalPrice) { System.out.println("恭喜!老客户享有9折优惠!"); originalPrice = originalPrice.multiply(new BigDecimal(0.9)).setScale(2,BigDecimal.ROUND_HALF_UP); return originalPrice; } }
package com.slp; import java.math.BigDecimal; //VIP客户的报价策略实现 public class VIPCustomerQuoteStrategy implements IQuoteStrategy { @Override public BigDecimal getPrice(BigDecimal originalPrice) { System.out.println("恭喜!VIP客户享有8折优惠!"); originalPrice = originalPrice.multiply(new BigDecimal(0.8)).setScale(2,BigDecimal.ROUND_HALF_UP); return originalPrice; } }
package com.slp; import java.math.BigDecimal; //报价上下文角色 public class QuoteContext { //持有一个具体的报价策略 private IQuoteStrategy quoteStrategy; //注入报价策略 public QuoteContext(IQuoteStrategy quoteStrategy){ this.quoteStrategy = quoteStrategy; } //回调具体报价策略的方法 public BigDecimal getPrice(BigDecimal originalPrice){ return quoteStrategy.getPrice(originalPrice); } }
package com.slp; import java.math.BigDecimal; //外部客户端 public class Client { public static void main(String[] args) { //1.创建老客户的报价策略 IQuoteStrategy oldQuoteStrategy = new OldCustomerQuoteStrategy(); //2.创建报价上下文对象,并设置具体的报价策略 QuoteContext quoteContext = new QuoteContext(oldQuoteStrategy); //3.调用报价上下文的方法 BigDecimal price = quoteContext.getPrice(new BigDecimal(100)); System.out.println("折扣价为:" +price); } }
考虑开闭原则,新推出折扣,增加策略实现。
package com.slp; import java.math.BigDecimal; //MVP客户的报价策略实现 public class MVPCustomerQuoteStrategy implements IQuoteStrategy { @Override public BigDecimal getPrice(BigDecimal originalPrice) { System.out.println("哇偶!MVP客户享受7折优惠!!!"); originalPrice = originalPrice.multiply(new BigDecimal(0.7)).setScale(2,BigDecimal.ROUND_HALF_UP); return originalPrice; } }
调用修改:
package com.slp; import java.math.BigDecimal; //外部客户端 public class Client { public static void main(String[] args) { //创建MVP客户的报价策略 IQuoteStrategy mvpQuoteStrategy = new MVPCustomerQuoteStrategy(); //创建报价上下文对象,并设置具体的报价策略 QuoteContext quoteContext = new QuoteContext(mvpQuoteStrategy); //调用报价上下文的方法 BigDecimal price = quoteContext.getPrice(new BigDecimal(100)); System.out.println("折扣价为:" +price); } }
深入理解:
策略模式的作用:把具体的算法实现从业务逻辑中剥离出来,作为一系列独立算法类,使得他们可以相互替换。
策略模式的着重点:不是如何来实现算法,而是如何组织调用这些算法,从而让程序更加灵活,可扩展。
策略模式就是把各个平等的具体实现进行抽象,封装为独立的算法类,然后通过上下文和具体的算法类来进行交互,各个策略算法都是平等的,地位是一样的,正是由于各个算法的平等性,所以他们才是相互替换的,虽然我们可以动态切换各个策略,但是同一时刻只能使用一个策略。
策略和上下文的关系:
在策略模式中,一般情况下都是上下文持有策略的引用,以进行对具体策略的调用,但是具体的策略对象也可以从上下文中获取所需数据,可以将上下文当做参数传入导具体策略中,具体策略通过回调上下文中的方法来获取其所需的数据。
eg:支付策略
1 //支付策略接口 2 public interface PayStrategy { 3 //在支付策略接口的支付方法中含有支付上下文作为参数,以便在具体的支付策略中回调上下文中的方法获取数据 4 public void pay(PayContext ctx); 5 }
//人民币支付策略 public class RMBPay implements PayStrategy { @Override public void pay(PayContext ctx) { System.out.println("现在给:"+ctx.getUsername()+" 人民币支付 "+ctx.getMoney()+"元!"); } }
//美金支付策略 public class DollarPay implements PayStrategy { @Override public void pay(PayContext ctx) { System.out.println("现在给:"+ctx.getUsername()+" 美金支付 "+ctx.getMoney()+"dollar !"); } }
//支付上下文,含有多个算法的公有数据 public class PayContext { //员工姓名 private String username; //员工的工资 private double money; //支付策略 private PayStrategy payStrategy; public void pay(){ //调用具体的支付策略来进行支付 payStrategy.pay(this); } public PayContext(String username, double money, PayStrategy payStrategy) { this.username = username; this.money = money; this.payStrategy = payStrategy; } public String getUsername() { return username; } public double getMoney() { return money; } }
//外部客户端 public class Client { public static void main(String[] args) { //创建具体的支付策略 PayStrategy rmbStrategy = new RMBPay(); PayStrategy dollarStrategy = new DollarPay(); //准备小王的支付上下文 PayContext ctx = new PayContext("小王",30000,rmbStrategy); //向小王支付工资 ctx.pay(); //准备Jack的支付上下文 ctx = new PayContext("jack",10000,dollarStrategy); //向Jack支付工资 ctx.pay(); } }
增加一个银行账户的支付策略:
显然我们应该新增一个支付找银行账户的策略实现,由于需要从上下文中获取数据,为了不修改已有的上下文,我们可以通过继承已有的上下文来扩展一个新的带有银行账户的上下文,然后再客户端中使用新的策略实现和带有银行账户的上下文,这样之前已有的实现完全不需要改动,遵守了开闭原则。
//银行账户支付 public class AccountPay implements PayStrategy { @Override public void pay(PayContext ctx) { PayContextWithAccount ctxAccount = (PayContextWithAccount) ctx; System.out.println("现在给:"+ctxAccount.getUsername()+"的账户:"+ctxAccount.getAccount()+" 支付工资:"+ctxAccount.getMoney()+" 元!"); } }
//带银行账户的支付上下文 public class PayContextWithAccount extends PayContext { //银行账户 private String account; public PayContextWithAccount(String username, double money, PayStrategy payStrategy,String account) { super(username, money, payStrategy); this.account = account; } public String getAccount() { return account; } }
//外部客户端 public class Client { public static void main(String[] args) { //创建具体的支付策略 PayStrategy rmbStrategy = new RMBPay(); PayStrategy dollarStrategy = new DollarPay(); //准备小王的支付上下文 PayContext ctx = new PayContext("小王",30000,rmbStrategy); //向小王支付工资 ctx.pay(); //准备Jack的支付上下文 ctx = new PayContext("jack",10000,dollarStrategy); //向Jack支付工资 ctx.pay(); //创建支付到银行账户的支付策略 PayStrategy accountStrategy = new AccountPay(); //准备带有银行账户的上下文 ctx = new PayContextWithAccount("小张",40000,accountStrategy,"1234567890"); //向小张的账户支付 ctx.pay(); } }
当然有了,上面的实现方式是策略实现所需要的数据都是从上下文中获取,因此扩展了上下文;现在我们可以不扩展上下文,直接从策略实现内部来获取数据,看下面的实现:
//支付到银行账户的策略 public class AccountPay2 implements PayStrategy { //银行账户 private String account; public AccountPay2(String account) { this.account = account; } @Override public void pay(PayContext ctx) { System.out.println("现在给:"+ctx.getUsername()+"的账户:"+getAccount()+" 支付工资:"+ctx.getMoney()+" 元!"); } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } }
//外部客户端 public class Client { public static void main(String[] args) { //创建具体的支付策略 PayStrategy rmbStrategy = new RMBPay(); PayStrategy dollarStrategy = new DollarPay(); //准备小王的支付上下文 PayContext ctx = new PayContext("小王",30000,rmbStrategy); //向小王支付工资 ctx.pay(); //准备Jack的支付上下文 ctx = new PayContext("jack",10000,dollarStrategy); //向Jack支付工资 ctx.pay(); //创建支付到银行账户的支付策略 PayStrategy accountStrategy = new AccountPay2("1234567890"); //准备上下文 ctx = new PayContext("小张",40000,accountStrategy); //向小张的账户支付 ctx.pay(); } }
实现方式的区别:
扩展上下文实现:
优点:具体的策略实现风格统一,策略实现所需要的数据都从上下文中获取,在上下文中添加的数据,可以视为公共的数据,其他的策略实现也可以使用。
缺点:如果某些数据只是特定的策略实现需要,大部分的策略实现不需要,那这些数据有浪费之嫌,另外如果们每次添加算法数据都扩展上下文,很容易导致上下文的层级很复杂。
在具体实现策略上添加所需要的数据的实现:
有点:容易想到,实现简单
缺点:与其它策略的实现风格不一致,其它的策略实现所需数据都来自上下文,而这个策略实现一部分数据都来自自身,一部分数据来自于上下文;外部在使用这个策略实现的时候也和其它的策略实现不一致了,难以以一个统一的方式动态的切换策略实现。
策略模式在JDK中的应用:
在多线程编程中,我们经常使用线程池来管理线程,以减缓线程频繁的创建和销毁带来的资源的浪费,在创建线程池的时候,经常使用一个工厂类来创建线程池Executors,实际上Executors的内部使用的是类ThreadPoolExecutor.它有一个最终的构造函数如下:
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0) throw new IllegalArgumentException(); if (workQueue == null || threadFactory == null || handler == null) throw new NullPointerException(); this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }
corePoolSize:线程池中的核心线程数量,即使这些线程没有任务干,也不会将其销毁。
maximumPoolSize:线程池中的最多能够创建的线程数量。
keepAliveTime:当线程池中的线程数量大于corePoolSize时,多余的线程等待新任务的最长时间。
unit:keepAliveTime的时间单位。
workQueue:在线程池中的线程还没有还得及执行任务之前,保存任务的队列(当线程池中的线程都有任务在执行的时候,仍然有任务不断的提交过来,这些任务保存在workQueue队列中)。
threadFactory:创建线程池中线程的工厂。
handler:当线程池中没有多余的线程来执行任务,并且保存任务的多列也满了(指的是有界队列),对仍在提交给线程池的任务的处理策略。
RejectedExecutionHandler是一个策略接口,用子啊当线程池中没有多余的线程来执行任务,并且报错的任务的队列也满了(指的是有界队列),对仍在提交给线程池的任务的处理策略。
public interface RejectedExecutionHandler { /** *当ThreadPoolExecutor的execut方法调用时,并且ThreadPoolExecutor不能接受一个任务Task时,该方法就有可能被调用。 * 不能接受一个任务Task的原因:有可能是没有多余的线程来处理,有可能是workqueue队列中没有多余的位置来存放该任务,该方法有可能抛出一个未受检的异常RejectedExecutionException * @param r the runnable task requested to be executed * @param executor the executor attempting to execute this task * @throws RejectedExecutionException if there is no remedy */ void rejectedExecution(Runnable r, ThreadPoolExecutor executor); }
该策略有四个实现类:
AbortPolicy:该策略是直接将提交的任务抛弃掉,并抛出RejectedExecutionException异常。
/** * A handler for rejected tasks that throws a * <tt>RejectedExecutionException</tt>. */ public static class AbortPolicy implements RejectedExecutionHandler { /** * Creates an <tt>AbortPolicy</tt>. */ public AbortPolicy() { } /** * Always throws RejectedExecutionException. * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task * @throws RejectedExecutionException always. */ public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { throw new RejectedExecutionException(); } }
DiscardPolicy:该策略也是将任务抛弃掉(对提交的任务不管不问,什么也不做),不过并不抛出异常。
/** * A handler for rejected tasks that silently discards the * rejected task. */ public static class DiscardPolicy implements RejectedExecutionHandler { /** * Creates a <tt>DiscardPolicy</tt>. */ public DiscardPolicy() { } /** * Does nothing, which has the effect of discarding task r. * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task */ public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { } }
DiscardOldestPolicy:该测试时当执行器未关闭时,从任务队列workQueue中取出第一个任务,并抛弃这一个任务,进而有空间存储刚给提交的任务,使用该策略要小心,因为他会直接抛弃之前的任务。
/** * A handler for rejected tasks that discards the oldest unhandled * request and then retries <tt>execute</tt>, unless the executor * is shut down, in which case the task is discarded. */ public static class DiscardOldestPolicy implements RejectedExecutionHandler { /** * Creates a <tt>DiscardOldestPolicy</tt> for the given executor. */ public DiscardOldestPolicy() { } /** * Obtains and ignores the next task that the executor * would otherwise execute, if one is immediately available, * and then retries execution of task r, unless the executor * is shut down, in which case task r is instead discarded. * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task */ public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { e.getQueue().poll(); e.execute(r); } } }
CallerRunsPolicy:该策略并没有抛弃任何的任务,由于线程池中已经没有了多余的线程来分配该任务,改策略是当前线程(调用者线程)中直接执行该任务。
/** * A handler for rejected tasks that runs the rejected task * directly in the calling thread of the {@code execute} method, * unless the executor has been shut down, in which case the task * is discarded. */ public static class CallerRunsPolicy implements RejectedExecutionHandler { /** * Creates a {@code CallerRunsPolicy}. */ public CallerRunsPolicy() { } /** * Executes task r in the caller's thread, unless the executor * has been shut down, in which case the task is discarded. * * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task */ public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { r.run(); } } }
类ThreadPoolExecutor中持有一个RejectedExecutionHandler接口的引用,以便在构造函数中可以由外部客户端自己制定具体的策略并注入。
策略模式的优点:
1.策略模式的功能就是通过抽象、封装来定义一系列的算法,使得这些算法可以相互替换,所以为这些算法定义一个公共的接口,以约束这些算法的功能实现。如果这些算法具有公共的功能,可以将接口变为抽象类,将公共功能放到抽象父类里面。
2.策略模式的一系列算法是可以相互替换的、是平等的,写在一起就是if-else组织结构,如果算法实现里又有条件语句,就构成了多重条件语句,可以用策略模式,避免这样的多重条件语句。
3.扩展性更好:在策略模式中扩展策略实现非常的容易,只要新增一个策略实现类,然后在使用策略实现的地方,使用这个新的策略实现就好了。
策略模式的缺点:
1.客户端必须了解所有的策略,清楚它们的不同:
如果由客户端来决定使用何种算法,那客户端必须知道所有的策略,清楚各个策略的功能和不同,这样才能做出正确的选择,但是这暴露了策略的具体实现。
2.增加了对象的数量:
由于策略模式将每个具体的算法都单独封装为一个策略类,如果可选的策略有很多的话,那对象的数量也会很多。
3.只适合偏平的算法结构:
由于策略模式的各个策略实现是平等的关系(可相互替换),实际上就构成了一个扁平的算法结构。即一个策略接口下面有多个平等的策略实现(多个策略实现是兄弟关系),并且运行时只能有一个算法被使用。这就限制了算法的使用层级,且不能被嵌套。
策略模式的本质:
分离算法,选择实现。
如果你仔细思考策略模式的结构和功能的话,就会发现:如果没有上下文,策略模式就回到了最基本的接口和实现了,只要是面向接口编程,就能够享受到面向接口编程带来的好处,通过一个统一的策略接口来封装和分离各个具体的策略实现,无需关系具体的策略实现。
貌似没有上下文什么事,但是如果没有上下文的话,客户端就必须直接和具体的策略实现进行交互了,尤其是需要提供一些公共功能或者是存储一些状态的时候,会大大增加客户端使用的难度;引入上下文之后,这部分工作可以由上下文来完成,客户端只需要和上下文进行交互就可以了。这样可以让策略模式更具有整体性,客户端也更加的简单。
策略模式体现了开闭原则:策略模式把一系列的可变算法进行封装,从而定义了良好的程序结构,在出现新的算法的时候,可以很容易的将新的算法实现加入到已有的系统中,而已有的实现不需要修改。
策略模式体现了里氏替换原则:策略模式是一个扁平的结构,各个策略实现都是兄弟关系,实现了同一个接口或者继承了同一个抽象类。这样只要使用策略的客户端保持面向抽象编程,就可以动态的切换不同的策略实现以进行替换。