Strategy mode
I. what is the strategic model
As a software design pattern, policy pattern refers to the behavior of an object, but in different scenarios, the behavior has different implementation algorithms. For example, everyone has to "pay personal income tax", but "pay personal income tax in the United States" and "pay personal income tax in China" have different tax calculation methods.
Strategy, which defines a set of algorithms, encapsulates each algorithm and makes them interchangeable.
The UML structure diagram is as follows:
Context is the context, which maintains a reference to the Strategy object; Strategy is the policy class, which is used to define the public interface of all supported algorithms; ConcreteStrategy is the specific policy class, which encapsulates the specific algorithm or behavior and inherits from Strategy.
1. Context context
Context context role, also known as context encapsulation role, serves as a link between the preceding and the following, shielding the direct access of high-level modules to policies and algorithms, and encapsulating possible changes.
public class Context { Strategy strategy; public Context(Strategy strategy) { this.strategy = strategy; } //Context interface public void contextInterface() { strategy.algorithmInterface(); } }
2. Strategic role
Abstract policy role is an abstraction of policy and algorithm family, usually an interface, which defines the methods and attributes that each policy or algorithm must have. Algorithm means "algorithm".
public abstract class Strategy { //Algorithm method public abstract void algorithmInterface(); }
3. Specific strategic roles
It is used to implement the operation in the abstract strategy, that is, to implement the specific algorithm, and print is used to replace it below. There are three concretestrategies in the test class. The other two classes are the same as ConcreteStrategyA, so we will not repeat them.
public class ConcreteStrategyA extends Strategy { @Override public void algorithmInterface() { System.out.println("algorithm A Realization"); } }
4. Client client
Next, change the strategy in turn and test the strategy mode.
public class Client { public static void main(String[] args) { Context context; context = new Context(new ConcreteStrategyA()); context.contextInterface(); context = new Context(new ConcreteStrategyB()); context.contextInterface(); context = new Context(new ConcreteStrategyC()); context.contextInterface(); } } /**test result Algorithm A implementation Algorithm B implementation Algorithm C implementation */
II. Application, advantages and disadvantages of strategy mode
Application scenario
- Multiple classes only have slightly different scenarios in algorithm or behavior
- Scenarios where the algorithm needs to switch freely
- Scenarios that need to mask algorithm rules
Advantage:
- Algorithm can switch freely
- Avoid using multiple conditional judgments (we may use multiple conditional statements if we do not use the policy mode, which is not conducive to maintenance)
- It has good scalability and only needs to implement the interface to add a strategy
Disadvantages:
- The number of policy classes will increase, and each policy is a class, so the possibility of reuse is very small
- All policy classes need to be exposed
III. strategic model cases
Let's simulate a scene of going home from work. We need to choose different means of transportation to go home
1. Context class
First, a TravelContext object is declared, and the specific vehicle is passed in through the construction method.
public class TravelContext { private Vehicle vehicle; public TravelContext(Vehicle vehicle){ this.vehicle = vehicle; } public void goHome(){ if (vehicle!=null){ vehicle.travel(); } } }
2. Vehicle abstract class
public interface Vehicle { void travel(); }
3. Different vehicles
public class Bus implements Vehicle { public void travel() { System.out.println("Bus ride"); } } public class Car implements Vehicle { public void travel() { System.out.println("by car "); } } public class Taxi implements Vehicle { public void travel() { System.out.println("Take a taxi"); } }
4. Client
Now write a simple program to test the code written above.
public class Client { public static void main(String[] args) { TravelContext travelContext = null; System.out.println("Please choose the way to go home:1.Car 2.Taxi 3.Bus"); Scanner scanner = new Scanner(System.in); int input = scanner.nextInt(); switch (input){ case 1: travelContext = new TravelContext(new Car()); travelContext.goHome(); break; case 2: travelContext = new TravelContext(new Taxi()); travelContext.goHome(); break; case 3: travelContext = new TravelContext(new Bus()); travelContext.goHome(); break; default: System.out.println("Please input 1/2/3"); break; } } } /**test result Please choose the vehicle to take home: 1. Car 2. Taxi 3. Bus 3 Bus ride */