[Strategy-behavioral pattern of 23 frequently tested design patterns in Java interviews]
Knowledge Review:
The design patterns we talked about before are here:
[The singleton pattern of the most common design pattern in interviews]
[Interview the most common design pattern factory pattern]
[Memento (Memento) - Behavioral Patterns of 23 Design Patterns Commonly Tested in Interviews in Java]
[Observer mode (Observer)-behavioral mode of 23 design patterns frequently tested in interviews in Java]
[Template-behavioral pattern of 23 design patterns frequently tested in interviews in Java]
[State mode (State)-behavioral mode of 23 design patterns frequently tested in interviews in Java]
The next thing we want to learn is: [Strategy-behavioral pattern of 23 design patterns frequently tested in interviews in Java].
strategy mode
The core idea of the strategy pattern is: In the strategy pattern, we create objects representing various strategies and a context object whose behavior changes as the strategy object changes. The strategy object changes the execution algorithm of the context object.
Strategy Mode VS State Mode
Whether in terms of code implementation or class diagrams, the strategy pattern and the state pattern are almost the same, but they are essentially different. The strategy pattern is to accomplish one thing with different implementations and different behaviors; but the state pattern is The different states corresponding to an object still complete one thing as a whole. This thing corresponds to different states. You can simply understand it as only one strategy, and this strategy corresponds to multiple states.
solved problem
Complicated and difficult to maintain using if...else when there are multiple algorithms that are similar.
Scenarios for applying the strategy pattern
Different actions that do the same thing.
Scenarios about the application of the strategy pattern in life
- The way to travel, choose to ride a bicycle or take a car, each way of travel is a strategy.
- Members, ordinary users, super members, etc. correspond to different discount strategies.
- and many more. . . and many more. . . .
Common usage scenarios in production development
- In Spring framework, Resource interface, resource access strategy
- Different strategies for solving an algorithmic problem.
- and many more. . . . . and many more. . . .
Advantages and Disadvantages of Strategy Pattern
advantage
- Algorithms can be switched freely.
- Avoid using multiple conditional judgments.
- Good scalability.
shortcoming
- There will be more and more strategy classes, which is not conducive to management and maintenance in the later stage.
- All policy classes need to be exposed to the outside world, which is not secure enough.
core role
Define the Strategy of the rule interface, the concrete class that implements the strategy of the rule interface, and the class Context that uses a certain strategy.
UML class diagram
implementation code
Let's take such a chestnut: In an algorithm competition, there is a very interesting topic and there are multiple solutions: Assumption: Solution 1: Brute-force method Solution 2: Double pointer Solution 3: Line segment tree
interface strategy class
package com.strategy; interface Strategy { void solveProblemDifferentsMethods(); }
Concrete implementation class 1
package com.strategy; class BFStrategy implements Strategy{ @Override public void solveProblemDifferentsMethods() { // TODO Auto-generated method stub System.out.println("I solved this problem using a brute force solution. . . . BFStrategy"); } }
Concrete implementation strategy class 2
package com.strategy; class TwoPonterStrategy implements Strategy{ @Override public void solveProblemDifferentsMethods() { // TODO Auto-generated method stub System.out.println("I solved this problem using the double pointer solution. . . . TwoPonterStrategy"); } }
Concrete implementation strategy class 3
package com.strategy; class SegmentTreeStrategy implements Strategy{ @Override public void solveProblemDifferentsMethods() { // TODO Auto-generated method stub System.out.println("I used the line segment tree solution to solve this problem. . . . SegmentTreeStrategy"); } }
Set the class that uses the policy
package com.strategy; class Context{ private Strategy strategy; public Context(Strategy strategy){ this.strategy = strategy; } public void exec(){ strategy.solveProblemDifferentsMethods(); } }
test code
package com.strategy; public class Main { public static void main(String[] args){ Strategy strategy=new BFStrategy(); Context context=new Context(strategy); context.exec(); System.out.println("Option 1 was successfully resolved by violent law. . . . ."); strategy=new TwoPonterStrategy(); context=new Context(strategy); context.exec(); System.out.println("Scheme 2 The double pointer method is successfully solved. . . . ."); strategy=new SegmentTreeStrategy(); context=new Context(strategy); context.exec(); System.out.println("The solution line segment tree method is successfully solved. . . . ."); } }
Running results show:
Well, here [Strategy-behavioral mode of 23 design patterns that are frequently tested in Java interviews] is over, and the 23 design patterns are continuously updated and summarized.