Java design patterns: Strategy pattern

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/sinat_38393872/article/details/97671562

This article is a summary of their own learning, learning sources are lying design patterns, Cheng Jie with Tsinghua University Publishing House.

 

Many models are designed to comply with the concept of this high cohesion, low coupling, the strategy pattern is no exception.

When executing a business, there are different execution strategies, such as promotions, discounts, full cut can also be filled to a certain amount of redemption goods, which are specific sales practices. In these specific promotional code behavior should be different logic code. Front to back-end code is simply classified, it should belong to the back-end code, belong to the business layer.

If you want to adhere to the design concept of low coupling, and that the front end when the calling code promotional activities, should be as simple as possible, preferably just a word.

Strategy pattern is to meet the needs of the above scenarios, let's take a look at the strategy pattern UML diagram.

 

Context declared object Strategy abstract class, which contextInterface () is for the front-end called to perform specific strategies, whether it is to perform what strategies, the front end can be unified call this method to execute strategy to achieve before and after the end of the separation of purpose . Context class is for the convenience of front-end calls and design. Context code is as follows.

public class Context{
    private Strategy strategy;
    //Context的构造函数,传进具体的Strategy,就能得到具体的执行策略
    public Context() {Strategy strategy) {
        this.strategy = strategy;
    }
    
    //前端传进具体的Strategy,然后统一调用该方法,而不用根据策略的不同调用的方法名也不同
    public static void contextInterface(){
        strategy.algorithmInterface();
    }
}

Strategy class code as follows

public abstract class Strategy{
    public abstract void AlgorithmInterface();
}

Specific strategies class inherits Strategy, code is as follows;

public class ConcreteStrategyA{
    public void algorithmInterface(){
        //定义具体的策略
    }
}

The design, front-end code no matter what strategy to use, can be written in a uniform manner, as follows

Context context = new Context(new ConcreteStrategyA());
context.concreteInterface();

Finally, to sum up, the policy mode is to allow the front end in a uniform code calls a different policy approach. These strategies should be a specific implementation methods abstract strategy, as just an example of the promotion is an abstract strategy, discount, full reduction of specific behavioral strategies equation.

In fact, the policy can also optimize it, the code for specific policy class can be obtained by factory methods, I can see the factory on another article.

https://blog.csdn.net/sinat_38393872/article/details/94738356

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/sinat_38393872/article/details/97671562