LieBrother原文:
行为型模式:策略模式java
十一大行为型模式之五:策略模式。git
姓名 :策略模式github
英文名 :Strategy Pattern算法
价值观 :集计谋于一身设计模式
我的介绍 :
Define a family of algorithms,encapsulate each one,and make them interchangeable.
定义一组算法,将每一个算法都封装起来,而且使它们之间能够互换。
(来自《设计模式之禅》)ide
先看一张拍得很差看的图片ui
天天上完班回到家第一件事情是干啥?有人一进门就躺在沙发上闭目养神、有人一进门躺在沙发上玩手机、有人一进门就陪本身的小宠物玩等等。而我进门第一件事就是洗澡,洗完澡很容易就把一成天的疲惫感给消除掉,而后就能够开始美好的下班时光。现实没那么美好,洗完澡后还要洗衣服,大学手洗了 4 年的衣服,一出来工做,宿舍第一必需品就是洗衣机。细看洗衣机,有不少种洗衣类型,好比:标准、大物、快洗、轻柔。洗衣类型的区别在于洗衣服的过程不同,洗衣过程包括有浸泡、洗涤、漂洗、脱水,还有洗衣服的时间也不同。细想能够发现这 4 种洗衣类型实际上是洗衣服的 4 种不一样的策略,也便是 4 种不一样的算法。根据这个思路,咱们能够用代码实现它,定义一个接口 WashingStrategy 定义洗衣服类型,而这些类型都有各自的洗衣过程,好比标准洗衣类型就包括浸泡、洗涤、漂洗、脱水,而快洗则只包括洗涤、漂洗、脱水。而咱们洗衣服则须要选择某个洗衣类型后,洗衣机就开始工做了。过程以下代码所示。this
public class StrategyTest { public static void main(String[] args) { WashingStrategy washingStrategy = new StandardWashingStrategy(); WashingMachine washingMachine = new WashingMachine(washingStrategy); washingMachine.washingClothes(); } } /** * 洗衣类型 */ interface WashingStrategy { void washing(); } /** * 洗衣机 */ class WashingMachine { private WashingStrategy washingStrategy; public WashingMachine(WashingStrategy washingStrategy) { this.washingStrategy = washingStrategy; } public void washingClothes() { this.washingStrategy.washing(); } } /** * 标准 */ class StandardWashingStrategy implements WashingStrategy{ @Override public void washing() { System.out.println("标准流程:"); System.out.println("[浸泡] 10 分钟"); System.out.println("[洗涤] 2 次,每次 15 分钟"); System.out.println("[漂洗] 1 次,每次 10 分钟"); System.out.println("[脱水] 5 分钟"); System.out.println("总共耗时:55 分钟"); } } /** * 快洗 */ class QuickWashingStrategy implements WashingStrategy { @Override public void washing() { System.out.println("快洗流程:"); System.out.println("[洗涤] 1 次,每次 10 分钟"); System.out.println("[漂洗] 1 次,每次 10 分钟"); System.out.println("[脱水] 5 分钟"); System.out.println("总共耗时:25 分钟"); } } /** * 大物 */ class BigClothesWashingStrategy implements WashingStrategy { @Override public void washing() { System.out.println("大物流程:"); System.out.println("[浸泡] 30 分钟"); System.out.println("[洗涤] 3 次,每次 15 分钟"); System.out.println("[漂洗] 2 次,每次 10 分钟"); System.out.println("[脱水] 5 分钟"); System.out.println("总共耗时:100 分钟"); } } /** * 轻柔 */ class SoftWashingStrategy implements WashingStrategy { @Override public void washing() { System.out.println("轻柔流程:"); System.out.println("[浸泡] 10 分钟"); System.out.println("[漂洗] 2 次,每次 15 分钟"); System.out.println("[脱水] 5 分钟"); System.out.println("总共耗时:45 分钟"); } } 标准流程: [浸泡] 10 分钟 [洗涤] 2 次,每次 15 分钟 [漂洗] 1 次,每次 10 分钟 [脱水] 5 分钟 总共耗时:55 分钟
是否是感受策略模式很简单呢?上面代码就是完整的策略模式示例,是否是感受有些问题,这 4 种洗衣类型对象彻底暴露给了用户,这也是策略模式的缺点。每每策略模式不会单独使用,会和其余设计模式一块儿使用,好比和简单工厂模式一块儿使用就能够解决这个对外暴露对象的问题,看下面代码。设计
/** * 洗衣类型选择 */ class WashingFactory { public static WashingStrategy getWashingStrategy(String type) { if ("Quick".equals(type)) { return new QuickWashingStrategy(); } if ("BigClothes".equals(type)) { return new BigClothesWashingStrategy(); } if ("Soft".equals(type)) { return new SoftWashingStrategy(); } return new StandardWashingStrategy(); } } public class StrategyTest { public static void main(String[] args) { WashingStrategy washingStrategy2 = WashingFactory.getWashingStrategy("Soft"); WashingMachine washingMachine2 = new WashingMachine(washingStrategy2); washingMachine2.washingClothes(); } } 打印结果: 轻柔流程: [浸泡] 10 分钟 [漂洗] 2 次,每次 15 分钟 [脱水] 5 分钟 总共耗时:45 分钟
代码中使用 WashingFactory 来封装 4 种策略,使得策略没有对外暴露,咱们也了解到设计模式之间具备互补的关系,有些时候并非单独存在的。code
代码:
Strategy Pattern
策略模式是一个很好的封装各类复杂处理的设计模式,让使用者根据本身的选择来选中策略,而不用修改其余代码。当策略太多的时候,可能形成使用方变得复杂、难管理多个策略的问题,利用工厂方法模式能够很好的解决这个难题。这其中也是一个见招拆招的问题,设计模式在真正运用中也是这样子的,遇到问题使用恰当的设计模式去解决问题。
参考资料:《大话设计模式》、《设计模式之禅》
推荐阅读:
但愿文章对您有所帮助,设计模式系列会持续更新,感兴趣的同窗能够关注公众号:LieBrother,第一时间获取文章推送阅读,也能够一块儿交流,交个朋友。