Revisiting Design Patterns—Strategy Patterns

1. Background introduction

In the process of making products recently, I have done in-depth research on the topic discussion reply content according to the number of follow-up comments, likes, time, etc., and can be well optimized through the strategy mode.

2. Ideas & plans

  • 1. Introduction to Strategy Pattern
  • 2. Class diagram of strategy pattern
  • 3. Strategy pattern code
  • 4. Where the strategy mode can be optimized
  • 5. Transformation of example of strategy pattern

3. Process

1. Introduction to Strategy Pattern

Strategy mode: It defines the algorithm family and encapsulates them separately so that they can be replaced with each other. This mode allows changes in the algorithm to not affect the customers who use the algorithm.

2. Class diagram of strategy pattern

insert image description here

3. Strategy pattern code

package mark.strategy;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 14:44
 */
public abstract class Strategy {
    
    
    public abstract void Algorithmlnterface();
}

package mark.strategy;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 14:46
 */
public class ConcreteStrategyA extends Strategy{
    
    
    @Override
    public void Algorithmlnterface() {
    
    
        System.out.println("算法A的具体实现");
    }
}

package mark.strategy;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 14:47
 */
public class ConcreteStrategyB extends Strategy{
    
    
    @Override
    public void Algorithmlnterface() {
    
    
        System.out.println("算法B的具体实现");
    }
}

package mark.strategy;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 14:47
 */
public class ConcreteStrategyC extends Strategy{
    
    
    @Override
    public void Algorithmlnterface() {
    
    
        System.out.println("算法C的具体实现");
    }
}

package mark.strategy;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 14:45
 */
public class Context {
    
    
    private Strategy strategy;

    Context(Strategy strategy){
    
    
        this.strategy = strategy;
    }
    public void ContextInterafce(){
    
    
        strategy.Algorithmlnterface();
    }
}

package mark.strategy;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 14:48
 */
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Context context;
        context = new Context(new ConcreteStrategyA());
        context.ContextInterafce();

        context = new Context(new ConcreteStrategyB());
        context.ContextInterafce();

        context = new Context(new ConcreteStrategyC());
        context.ContextInterafce();

    }
}

4. Where the strategy mode can be optimized

The expansion of the strategy mode in the future requires modification of the client code; for the client, the principle of opening and closing is not satisfied.

5. Example transformation of strategy mode (configuration file + reflection)

package mark.strategy.transform;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 14:44
 */
public abstract class Strategy {
    
    
    public abstract void Algorithmlnterface();
}

package mark.strategy.transform;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 14:46
 */
public class ConcreteStrategyA extends Strategy {
    
    
    @Override
    public void Algorithmlnterface() {
    
    
        System.out.println("算法A的具体实现");
    }
}

package mark.strategy.transform;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 14:47
 */
public class ConcreteStrategyB extends Strategy {
    
    
    @Override
    public void Algorithmlnterface() {
    
    
        System.out.println("算法B的具体实现");
    }
}

package mark.strategy.transform;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 14:47
 */
public class ConcreteStrategyC extends Strategy {
    
    
    @Override
    public void Algorithmlnterface() {
    
    
        System.out.println("算法C的具体实现");
    }
}

package mark.strategy.transform;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 14:45
 */
public class Context {
    
    

    //将这个数据放到配置文件中
    static Map<String,String> config = new HashMap<>();
    static Map<String,Strategy> configBean = new HashMap<>();
    static {
    
    
        config.put("strategyA","mark.strategy.transform.ConcreteStrategyA");
        config.put("strategyB","mark.strategy.transform.ConcreteStrategyB");
        config.put("strategyC","mark.strategy.transform.ConcreteStrategyC");
        //预加载提前做好的策略
        for (Map.Entry<String,String> entry:config.entrySet()) {
    
    
            Class strategyClass = null;
            try {
    
    
                strategyClass = Class.forName(entry.getValue());
                configBean.put(entry.getKey(),(Strategy)strategyClass.newInstance());
            } catch (Exception e) {
    
    
                throw new RuntimeException(e);
            }

        }
    }

    private Strategy strategy;

    Context(String type){
    
    
        try {
    
    
            if(configBean.containsKey(type)){
    
    
                this.strategy = configBean.get(type);
            }else {
    
    
                Class strategyClass = Class.forName(config.get(type));
                this.strategy = (Strategy)strategyClass.newInstance();
            }
        } catch (Exception e) {
    
    
            throw new RuntimeException(e);
        }
    }
    public void ContextInterafce(){
    
    
        strategy.Algorithmlnterface();
    }
}

package mark.strategy.transform;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 14:48
 */
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Context context;
        //前端通过下拉框选择不同的类型;下拉框中的值,也是通过配置文件进行读取的
        context = new Context("strategyA");
        context.ContextInterafce();
        context = new Context("strategyB");
        context.ContextInterafce();
        context = new Context("strategyC");
        context.ContextInterafce();
    }
}

Four. Summary

1. Through the comprehensive application of knowledge, the implementation of software engineering is more clear.
2. When to use strategy: single entry, single exit; use when internal needs to execute different strategies according to different conditions
3. The depth of the concept of strategy mode Understanding: Algorithm family = Strategy parent class, encapsulated separately = subclasses, so that they can be replaced with each other, algorithm changes = polymorphism, will not affect the client using the algorithm = the client satisfies the opening and closing principle but not thorough

Five, sublimation

Principles are meant to be strictly followed, and change never changes.

Guess you like

Origin blog.csdn.net/u013030601/article/details/132265959