美文网首页Java学习笔记Java 杂谈
一看就懂的设计模式--策略模式

一看就懂的设计模式--策略模式

作者: 不想当码农的程序员 | 来源:发表于2018-03-29 19:24 被阅读88次

    是什么?

    策略模式是指对一系列的算法定义,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。
    分析下定义:策略模式定义和封装了一系列的算法,它们是可以相互替换的,也就是说它们具有共性,而它们的共性就体现在策略接口的行为上,另外为了达到最后一句话的目的,也就是说让算法独立于使用它的客户而独立变化,我们需要让客户端依赖于策略接口。
    在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 context 对象。策略对象改变 context 对象的执行算法。

    类别

    行为型模式

    类图

    策略模式类图

    适用场景

    1、如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。
    2、一个系统需要动态地在几种算法中选择一种。
    3、如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现。

    关键

    所有的算法(行为)继承一个接口或抽象类,定义好共同的方法。具体的实现在具体的实现类中编写。然后在Content类中使用这个接口,也即是依赖这个接口。Content类中定义一个执行算法的方法,用接口.算法, 这个按照面向接口编程的方式。就能在不同场景执行不同策略(算法、行为)。

    注意事项

    如果一个系统的策略多于四个,就需要考虑使用混合模式,比如和享元模式结合,把常用的算法(也就是行为)类,用享元模式封装。

    常见案例

    1、jdk中的 Arrays的排序方法就是用了策略模式,Arrays.sort(排序策略),传入不同的排序策略就会出现不同的 排序结果,比如传入字典方式排序、从小到大排序,从大到下排序等等。

    优点

    1、算法可以自由切换。
    2、避免使用多重条件判断。(一个方法中有和多if else,这时候就可以把这方法里的具体逻辑用策略模式封装了,用户使用 时候直接传入对应的策略就行。就算加了新的策略,也不需要改 if else)
    3、扩展性良好。。

    缺点

    1、策略类会增多。
    2、所有策略类都需要对外暴露。

    实现步骤

    步骤 1

    创建一个接口,所有策略的共同接口,他定义了策略方法

    package com.pattern.strategy_pattern;
    
    /**
     * Created by chen on 2018/3/29.
     * <p>
     * Email 122741482@qq.com
     * <p>
     * Describe: 算法策略的接口,这里定义算法的抽象方法
     */
    public interface Strategy {
    
        /**
         * 对两个整数 做运算
         *
         * @param a
         * @param b
         */
        void algorithm(int a, int b);
    }
    
    
    

    步骤 2

    把具体的策略,实现起来,继承接口,实现接口定义的方法,也即是写具体的业务逻辑

    
    //   加法运算策略
    
    package com.pattern.strategy_pattern;
    
    /**
     * @author chen
     * @date 2018/3/29
     * <p>
     * Email 122741482@qq.com
     * <p>
     * Describe: 加法运算
     */
    public class AdditionStrategy implements Strategy {
        @Override
        public void algorithm(int a, int b) {
            System.out.println("加法运算 a+b=" + (a + b));
        }
    }
    
    
    
    //   减法运算策略
    
    package com.pattern.strategy_pattern;
    
    /**
     * @author chen
     * @date 2018/3/29
     * <p>
     * Email 122741482@qq.com
     * <p>
     * Describe: 减法运算
     */
    public class SubstractStrategy implements Strategy {
        @Override
        public void algorithm(int a, int b) {
            System.out.println("减法运行 a-b=" + (a - b));
        }
    }
    
    
    

    步骤3

    创建一个Content类,引入策略接口,执行策略方法。

    package com.pattern.strategy_pattern;
    
    /**
     * Created by chen on 2018/3/29.
     * <p>
     * Email 122741482@qq.com
     * <p>
     * Describe:
     */
    public class Context {
        private Strategy strategy;
    
        public Context(Strategy strategy) {
            this.strategy = strategy;
        }
    
        /**
         * 根据strategy的类型 执行 对应的 策略
         * @param a
         * @param b
         */
        public void executeStrategy(int a, int b) {
            strategy.algorithm(a, b);
        }
    
    }
    
    
    

    步骤4

    使用 Context 来查看当它改变策略 Strategy 时的行为变化。

    
    package com.pattern.strategy_pattern;
    
    /**
     * Created by chen on 2018/3/29.
     * <p>
     * Email 122741482@qq.com
     * <p>
     * Describe:
     */
    public class StrategyDemo {
        public static void main(String[] args) {
            Context context1 = new Context(new SubstractStrategy());
            context1.executeStrategy(1,2);
    
    
            Context context2 = new Context(new AdditionStrategy());
            context2.executeStrategy(1,2);
        }
    }
    
    

    步骤5

    查看输出

    
    减法运行 a-b=-1
    加法运算 a+b=3
    
    

    博客源码地址https://gitee.com/jamen/design-pattern

    我的官网http://guan2ye.com
    我的CSDN地址http://blog.csdn.net/chenjianandiyi
    我的简书地址http://www.jianshu.com/u/9b5d1921ce34
    我的githubhttps://github.com/javanan
    我的码云地址https://gitee.com/jamen/
    阿里云优惠券https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=vf2b5zld&utm_source=vf2b5zld

    阿里云教程系列网站http://aliyun.guan2ye.com

    1.png

    我的开源项目spring boot 搭建的一个企业级快速开发脚手架

    1.jpg

    相关文章

      网友评论

        本文标题:一看就懂的设计模式--策略模式

        本文链接:https://www.haomeiwen.com/subject/ourvcftx.html