The strategic pattern of JAVA design pattern

There is an opening and closing principle in the design principle (closing for modification and opening for addition). That's what strategy follows

Scene: May Day holiday is coming soon. Many people choose to go home. What kind of transportation should they choose? This kind of transportation is the strategy of going home.

1. Home strategy interface

/**
 * Abstract interface, home strategy
 */
abstract public interface AbstractGoHomeStrategy {
    void goHomeByWhat();
}

2. Train home strategy

/**
 * Take the train home, specific implementation strategy
 */
public class TrainStrategy implements AbstractGoHomeStrategy{
    @Override
    public void goHomeByWhat() {
        System.out.println("Take the train home");
    }
}

3. High speed rail home

/**
 * Take high-speed rail home, specific implementation strategy
 */
public class HighSpeedRailStrategy implements AbstractGoHomeStrategy {
    @Override
    public void goHomeByWhat() {
        System.out.println("Take the high speed Rail home");
    }
}

4. Holidays

/**
 * What are you going to do after May Day holiday
 */
public class Holiday {

    /**
     * get home
     */
    public void howGoHome(AbstractGoHomeStrategy ags){
        ags.goHomeByWhat();
    }

    /**
     * Study
     */
    public void study(){

    }
}

5. Test class

/**
 * Test class
 */
public class T {
    public static void main(String[] args) {
        Holiday h = new Holiday();
        AbstractGoHomeStrategy ags1 = new TrainStrategy();
        AbstractGoHomeStrategy ags2 = new HighSpeedRailStrategy();
        //Which strategy do you want to use to go home and pass it on ok
        h.howGoHome(ags2);
    }
}

Here, you can see that each time you want to use a policy, you need a new policy object, or you need to modify the code. If you don't want to modify it, please see the following

6. Add the policy configuration file config.properties under classpath

#Go home strategy
gohomeStrategy = com.srr.pd.strategy.TrainStrategy

7. Add property profile tool class

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 constructor to prevent instantiation */
    private PropertiesUtil (){};
    public static Properties propertie = null;
    static {
        propertie = new Properties();
        InputStream inputStream = PropertiesUtil.class.getResourceAsStream("/config.properties");
        //Solve Chinese code disorder
        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. After modifying the test class

package com.srr.pd.strategy;

/**
 * Test class
 */
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();
        }
        //Which policy do you want to use to go home and configure the home policy in the configuration file ok
        h.howGoHome(ags);
    }
}

Tags: Java

Posted on Mon, 27 Apr 2020 07:37:09 -0700 by koglakci