1. Strategy pattern
The strategy pattern embodies two very basic principles of object-oriented design
(1) Concept of packaging change
Explanation: TreeSet or TreeMap only knows to accept a Comparator, but we don't know the specific implementation class before we pass it in. It only knows an interface such as Comparator. Therefore, we can implement the Comparator interface ourselves, and then encapsulate it into our own comparison rules, that is, realize our own changes.
(2) Use interfaces in programming, not the implementation of interfaces
Interface oriented programming
2. Definition of policy mode
A set of algorithms is defined, each algorithm is encapsulated and interchangeable.
The policy pattern enables these algorithms to change without affecting each other when the client calls them.
3. Significance of strategic model
Policy pattern enables developers to develop software composed of many replaceable parts, and the relationship between each part is weak connection.
Explanation: TreeSet and Comparator are weakly connected, and each implementation class is also weakly connected.
The weak connection makes the software more scalable and easy to maintain; More importantly, it greatly improves the reusability of software.
4. Composition of strategic mode
Abstract policy role: policy class, usually implemented by an interface or abstract class.
Specific strategic roles: packaging specific algorithms and behaviors
Environment role: it holds a reference to a policy class and is finally called by the client. TreeSet or TreeMap
5. Implementation of policy mode
Policy pattern is to encapsulate each algorithm into an independent class with a common interface for a group of algorithms, so that they can be replaced with each other.
The policy mode allows the algorithm to change without affecting the client. Using policy patterns can separate behavior from environment.
The environment class is responsible for maintaining and querying the behavior class, and various algorithms are provided in the specific strategy. Since the algorithm is independent of the environment, the modification of the algorithm will not affect the environment and the client.
6. Preparation steps of policy mode
Writing steps of policy mode:
(1) Define a public interface for the policy object.
(2) Write a policy class that implements the above public interface.
(3) Save a reference to the policy object in the class that uses the policy object.
(4) In the class using the policy object, implement the set and get methods (injection) of the policy object or use the constructor to assign values.
7. Examples
package com.wenxiaowu.dp; import org.junit.Test; /** * Strategy mode */ public class StrategyTest { @Test public void tests() { AddStrategy addStrategy = new AddStrategy(); Context Context = new Context(addStrategy); int value = Context.caclulate(170, 120); System.out.println("addStrategy: " + value); SubtractStrategy subtractStrategy = new SubtractStrategy(); Context.setStrategy(subtractStrategy); int value1 = Context.caclulate(170, 120); System.out.println("subtractStrategy: " + value1); MultiplyStrategy multiplyStrategy = new MultiplyStrategy(); Context.setStrategy(multiplyStrategy); int value2 = Context.caclulate(170, 120); System.out.println("multiplyStrategy: " + value2); DivideStrategy divideStrategy = new DivideStrategy(); Context.setStrategy(divideStrategy); int value3 = Context.caclulate(170, 120); System.out.println("divideStrategy: " + value3); } } /** * context */ class Context { private Strategy strategy; public Context(Strategy strategy) { this.strategy = strategy; } public void setStrategy(Strategy strategy) { this.strategy = strategy; } public Strategy getStrategy() { return strategy; } /** * strategy I'll use whatever comes from outside */ public int caclulate(int a, int b) { return this.strategy.calculate(a, b); } } /** * Abstract policy class */ interface Strategy { int calculate(int a, int b); } /** * Addition strategy */ class AddStrategy implements Strategy { @Override public int calculate(int a, int b) { return a + b; } } /** * Division strategy */ class DivideStrategy implements Strategy { @Override public int calculate(int a, int b) { return a / b; } } /** * Multiplication strategy */ class MultiplyStrategy implements Strategy { @Override public int calculate(int a, int b) { return a * b; } } /** * Subtraction strategy */ class SubtractStrategy implements Strategy { @Override public int calculate(int a, int b) { return a - b; } }
8. Disadvantages of strategy mode
(1) The client must know all policy classes and decide which policy class to use.
(2) Cause a lot of policy classes.
Solution: adopt factory method