zoukankan      html  css  js  c++  java
  • 策略模式

    1.策略模式: 它定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户.

    2. uml类图

    3. 优点:
    (1)策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合.
    (2)策略模式的Strategy类层次为Context定义了一系列的可供重用的算法或行为,继承有助于析取出这些算法中的公共功能.

    (3)策略模式的优点是简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试.

    4. 缺点:
          1、客户端必须知道所有的策略类,并自行决定使用哪一个策略类。这就意味着客户端必须理解这些算法的区别,以便适时选择恰当的算法类。换言之,策略模式只适用于客户端知道所有的算法或行为的情况。
         
          2、 策略模式造成很多的策略类,每个具体策略类都会产生一个新类。有时候可以通过把依赖于环境的状态保存到客户端里面,而将策略类设计成可共享的,这样策略类实例可以被不同客户端使用。换言之,可以使用享元模式来减少对象的数量。
     
       5. 使用场景:  只要在分析过程中听到需要在不同时间应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性.
     具体实现:
     1 package strategy.strategy;
     2 
     3 /**
     4  * @author liuboren
     5  * @Title:抽象算法类
     6  * @Description:
     7  * @date 2018/5/30 14:52
     8  */
     9 public abstract class Strategy {
    10 
    11     //算法方法
    12     public abstract void  algorithmInterface();
    13 
    14 }
     1 package strategy.strategy.impl;
     2 
     3 import strategy.strategy.Strategy;
     4 
     5 /**
     6  * @author liuboren
     7  * @Title:具体算法类A
     8  * @Description:
     9  * @date 2018/5/30 14:53
    10  */
    11 public class ConcreteStrategyA extends Strategy {
    12 
    13     @Override
    14     public void algorithmInterface() {
    15         System.out.println("算法A实现");
    16     }
    17 }
     1 package strategy.strategy.impl;
     2 
     3 import strategy.strategy.Strategy;
     4 
     5 /**
     6  * @author liuboren
     7  * @Title:具体算法类B
     8  * @Description:
     9  * @date 2018/5/30 14:54
    10  */
    11 public class ConcreteStrategyB extends Strategy {
    12 
    13     @Override
    14     public void algorithmInterface() {
    15         System.out.println("算法B实现");
    16     }
    17 }
     1 package strategy.strategy.impl;
     2 
     3 import strategy.strategy.Strategy;
     4 
     5 /**
     6  * @author liuboren
     7  * @Title:具体算法类C
     8  * @Description:
     9  * @date 2018/5/30 14:55
    10  */
    11 public class ConcreteStrategyC extends Strategy {
    12     @Override
    13     public void algorithmInterface() {
    14         System.out.println("算法C实现");
    15     }
    16 }
     1 package strategy.context;
     2 
     3 import strategy.strategy.Strategy;
     4 
     5 /**
     6  * @author liuboren
     7  * @Title:调用算法类
     8  * @Description:
     9  * @date 2018/5/30 14:55
    10  */
    11 public class Context {
    12 
    13     private Strategy strategy;
    14 
    15     //初始化的时候传入具体的策略对象
    16     public Context(Strategy strategy) {
    17         this.strategy = strategy;
    18     }
    19 
    20     //根据具体的策略对象,调用其算法的方法
    21     public void contextInterface(){
    22         this.strategy.algorithmInterface();
    23 
    24     }
    25 }
    package strategy.test;
    
    import strategy.context.Context;
    import strategy.strategy.impl.ConcreteStrategyA;
    import strategy.strategy.impl.ConcreteStrategyB;
    import strategy.strategy.impl.ConcreteStrategyC;
    
    /**
     * @author liuboren
     * @Title:测试类
     * @Description:
     * @date 2018/5/30 14:57
     */
    public class Test {
            public static void main(String [] args){
                        Context context;
    
                        context = new Context(new ConcreteStrategyA());
                        context.contextInterface();
    
                        context = new Context(new ConcreteStrategyB());
                        context.contextInterface();
    
                        context = new Context(new ConcreteStrategyC());
                        context.contextInterface();
    
            }  
    }

    github:https://github.com/liuboren0617/designpatterns/tree/master/src/strategy



          
  • 相关阅读:
    "less is more",用"less”命令查看linux文本文件
    Linux命令"ls"进阶说明
    Linux文件权限说明
    Ubuntu14.04安装Ruby2.2方法
    Ubuntu查找软件命令
    Using If/Truth Statements with pandas
    Categorical Data
    DataFrame.loc的区间
    pandas学习(一)
    JDBC编程之事务处理
  • 原文地址:https://www.cnblogs.com/xisuo/p/9221860.html
Copyright © 2011-2022 走看看