Behavioral patterns - Strategy pattern

Strategy Mode

In Strategy Mode (Strategy Pattern), a class of behavior or algorithm can be changed at runtime. In strategy mode, we create a Context object represent objects and a variety of behavioral change strategies as policy objects that change. Strategy execution mode change algorithms Context object.

Introduction

It is intended to: define a set of algorithms, encapsulate them one by one, and makes them interchangeable.

The main solution: In a variety of algorithms similar case, if ... else brought complex and difficult to maintain.

When to use: a system has many, many classes, and distinguish them only their immediate behavior.

How to solve: these algorithms into a package of a class, arbitrarily replaced.

The key code: implement the same interface.

Application examples:  1, Zhuge Liang of good ideas, each kit is a strategy. 2, travel way to travel, cycling, by car, each way to travel is a strategy. 3, JAVA AWT in LayoutManager.

Advantages:  1, the algorithm can freely switch. 2, to avoid the use of multiple conditional. 3, good scalability.

Disadvantages:  1, the Strategy will increase. 2, all policy classes need of external exposure.

Usage scenarios:  1, if there is a system in which many classes, the difference between them only in their behavior, then use the strategy pattern allows an object can be dynamically select an action in a number of behavior. 2, a system need to dynamically choose one of several algorithms. 3. If an object has a lot of behavior, if that is not appropriate mode, these acts had to use multiple conditional selection statement to achieve.

Note: If the policy is a system of more than four, we need to consider the use of mixed-mode, problem-solving strategies class expansion.

achieve

We will create a custom activity of  Strategy  interface and implementation of the  Strategy  entity policy class interface. Context  is using a strategy of some kind.

StrategyPatternDemo , our demonstration classes use  Context  and policy objects to demonstrate changes in behavior Context when it configuring or using a policy change.

 

 1, to create an interface

public interface Strategy {
    public int doOperation(int num1, int num2);
}

2. Create a class that implements the interface entity

public class OperationAdd implements Strategy {

    @Override
    public int doOperation(int num1, int num2) {
        return num1 + num2;
    }
}
public class OperationSubtract implements Strategy{

    @Override
    public int doOperation(int num1, int num2) {
        return num1 - num2;
    }
}
public class OperationMultiply implements Strategy {

    @Override
    public int doOperation(int num1, int num2) {
        return num1 * num2;
    }
}

3. Create Context class

public class Context {
    private Strategy strategy;

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

    public int executeStrategy(int num1, int num2){
        return strategy.doOperation(num1, num2);
    }
}

4, Context view when it changes the behavior change strategies Strategy

public class StrategyPatternDemo {
    public static void main(String[] args) {
        Context context = new Context(new OperationAdd());
        System.out.println("10 + 5 = "+context.executeStrategy(10,5));

        context = new Context(new OperationSubtract());
        System.out.println("10 - 5 = "+context.executeStrategy(10,5));

        context = new Context(new OperationMultiply());
        System.out.println("10 * 5 = "+context.executeStrategy(10,5));

    }
}

Export

 

 Reference: https://www.runoob.com/design-pattern/strategy-pattern.html

Guess you like

Origin www.cnblogs.com/hoo334/p/12529490.html