• JAVA设计模式之策略模式


    设计原则中有个开闭原则(对修改关闭,对新增开放)。策略模式就是遵循了这种思想

    场景:五一快放假了,很多人选择回家,那么选择什么样的交通工具呢,这种交通工具就是回家的策略。

    1、回家策略接口

    /**
     * 抽象接口,回家的策略
     */
    abstract public interface AbstractGoHomeStrategy {
        void goHomeByWhat();
    }

    2、做火车回家策略

    /**
     * 坐火车回家,具体的实现策略
     */
    public class TrainStrategy implements AbstractGoHomeStrategy{
        @Override
        public void goHomeByWhat() {
            System.out.println("Take the train home");
        }
    }

    3、做高铁回家

    /**
     * 坐高铁回家,具体的实现策略
     */
    public class HighSpeedRailStrategy implements AbstractGoHomeStrategy {
        @Override
        public void goHomeByWhat() {
            System.out.println("Take the high speed Rail home");
        }
    }

    4、放假类

    /**
     * 五一快放假了准备干什么
     */
    public class Holiday {
    
        /**
         * 回家
         */
        public void howGoHome(AbstractGoHomeStrategy ags){
            ags.goHomeByWhat();
        }
    
        /**
         * 学习
         */
        public void study(){
    
        }
    }

    5、测试类

    /**
     * 测试类
     */
    public class T {
        public static void main(String[] args) {
            Holiday h = new Holiday();
            AbstractGoHomeStrategy ags1 = new TrainStrategy();
            AbstractGoHomeStrategy ags2 = new HighSpeedRailStrategy();
            //想用哪种策略回家把回家策略传进去就ok
            h.howGoHome(ags2);
        }
    }

    到这里,大家看到了,每次想用哪个策略都需要new一个策略对象,还是带来了代码的修改,如果做到不修改呢,请看下面

    6、在classpath下添加策略配置文件config.properties

    #回家策略
    gohomeStrategy = com.srr.pd.strategy.TrainStrategy

    7、添加属性配置文件工具类

    package com.srr.pd.strategy;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.Properties;
    
    public class PropertiesUtil {
    
        /* 私有构造方法,防止被实例化 */
        private PropertiesUtil (){};
        public static Properties propertie = null;
        static {
            propertie = new Properties();
            InputStream inputStream = PropertiesUtil.class.getResourceAsStream("/config.properties");
            //解决中文乱码
            BufferedReader buff = new BufferedReader(new InputStreamReader(inputStream));
    
            try {
                propertie.load(buff);
                inputStream.close();
            } catch (IOException e) {
            }
        }
    
        public static void main(String[] args) {
            System.out.println("gohomeStrategy = "+PropertiesUtil.propertie.get("gohomeStrategy"));
        }
    }

    8、修改测试类后

    package com.srr.pd.strategy;
    
    /**
     * 测试类
     */
    public class T {
    
        public static void main(String[] args) {
            Holiday h = new Holiday();
            AbstractGoHomeStrategy ags1 = new TrainStrategy();
            String strategy = (String) PropertiesUtil.propertie.get("gohomeStrategy");
            AbstractGoHomeStrategy ags = null;
            try {
                ags = (AbstractGoHomeStrategy) Class.forName(strategy).newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //想用哪种策略回家把回家策略在配置文件中配置好就ok
            h.howGoHome(ags);
        }
    }
  • 相关阅读:
    Spring Boot构建RESTful API与单元测试
    Spring Boot中使用Swagger2构建强大的RESTful API文档
    Intellij IDEA 一些不为人知的技巧
    Spring中@Controller和@RestController之间的区别
    Spring 中的default-lazy-init="true" 和 lazy-init="true"
    SpringMVC处理JSON
    建立一个简单的SpringMVC程序
    SpringMVC处理静态资源
    <mvc:annotation-driven/>与<mvc:default-servlet-handler/>之间的一个问题
    Spring AOP 简单理解
  • 原文地址:https://www.cnblogs.com/sx-bj-srr/p/strategy.html
Copyright © 2020-2023  润新知