Easy to use and analyze the strategy pattern example explanation

1 Introduction

In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.
——Quote from wikipedia

In computer programming, policy mode (also called the strategy pattern) is a behavioral software design patterns, which supports selection algorithm at runtime. Code for receiving operation instruction, rather than a single algorithm directly implemented, a series of instructions used in the algorithm.

[Image Uploading station outside ... (image-c98784-1573659608688)]

2. The characteristics of the strategy pattern

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.

3. Relevant examples

Examples Here we use the strategy pattern by arithmetic calculation, an algorithm for the base class, Subclasses implement arithmetic logic related, we have determined by selecting the appropriate subclass what algorithm.

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

class OperationAdd implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        System.out.println("add: " + num1 + "+" + num2);
        return num1 + num2;
    }
}

class OperationSubtract implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        System.out.println("substract: " + num1 + "-" + num2);
        return num1 - num2;
    }
}

class OperationMultiply implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        System.out.println("multiply: " + num1 + "*" + num2);
        return num1 * num2;
    }
}


class OperationDivide implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        System.out.println("divide: " + num1 + "/" + num2);
        return num1 / num2;
    }
}

class Context {

    private Strategy strategy;

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

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

public class Demo {

    public static void main(String[] args) {
        Context context1 = new Context(new OperationAdd());
        int result1 = context1.executeStrategy(9, 3);
        System.out.println("strategy result: "+result1+"\n");

        Context context2 = new Context(new OperationMultiply());
        int result2 = context2.executeStrategy(9, 3);
        System.out.println("strategy result: "+result2+"\n");

        Context context3 = new Context(new OperationSubtract());
        int result3 = context3.executeStrategy(9, 3);
        System.out.println("strategy result: "+result3+"\n");
    }
}
Output:
add: 9+3
strategy result: 12

multiply: 9*3
strategy result: 27

substract: 9-3
strategy result: 6

Guess you like

Origin www.cnblogs.com/charlypage/p/11854170.html