Hello, everyone, today is Friday, tomorrow will be the weekend, everyone relax, small mu also have to relax, but today still have to learn ah, today to share with you the content is “strategy mode”. As the title says, if not, I’ll take you out to dinner

What is strategic mode?

Strategy Pattern: Define a series of algorithms, encapsulate each algorithm, and make them interchangeable. A Policy pattern, also known as a Policy pattern, allows an algorithm to change independently of the customers that use it.

Ii. Applicable scenarios of policy Mode:

  1. Used when an application needs to implement a particular service or function and the application has multiple ways to implement it.

  2. Algorithms use data that customers shouldn’t know about. Policy patterns can be used to avoid exposing complex, algorithm-specific data structures.

  3. A class defines multiple behaviors, and these behaviors occur in the form of multiple conditional statements in the class’s operations. Move the related conditional branches into their respective Strategy classes to replace these conditional statements. **** (ps: To put it bluntly, “policy mode” can be used when there are too many if else’s in the code, and the conditional operations are similar)

Of course, the main purpose of the policy pattern is not to avoid if-else, but to follow the open and close principle of the program. (

Ps: In plain English, avoid adding if else to your code all the time. Every time you add a new process, you have to add if else. That doesn’t match the open and close principle

The open closed principle is: open for extension, closed for modification. Runs an extension of the program, but does not allow modification of the original program policy behavior.

Policy mode entry application

The focus of the strategy pattern is not how to implement the algorithm, but how to organize and invoke the algorithm, so that the program structure is more flexible, more maintainable, and more extensible.

First, let’s look at the composition of the strategy pattern:

Abstract policy roles: There is usually an interface or an abstract class implementation

2. Concrete policy roles: wrap related algorithms and behaviors

3. Environment role: An application that holds a policy class and is eventually called by the client

For example, Liu Bei was going to the east of the River to marry his wife. Before he left, Zhuge Liang gave Zhao Yun three clever ideas, saying that they could be opened separately in case of danger.

Three elements: three clever ideas (specific strategy class), a bag (environment class), Zhao Yun (caller).

AbstractStrategy Class

public interface AbstractStrategy {
     public void operate();
}
Copy the code

Three concrete implementation classes (ConcreteStrategy) :

Clever plan a: the first to wu

Public class implements AbstractStrategy {@override public void implement () {system.out.println (" implements AbstractStrategy; So that Sun Quan could not kill Liu Bei "); }}Copy the code

Clever plan two: beg wu too release

public class PlanTwo implements AbstractStrategy { @Override public void operate() { To lure Liu Bei back to his gentle hometown with a lie (Cao Cao fighting Jingzhou). ); }}Copy the code

Clever plan three: Madame Sun cut off the rear, blocking the pursuers

Public class implements AbstractStrategy {@override public void operate() {system.out.println (" implements AbstractStrategy; She is sun Quan's sister, and the generals of Wu fear her." ); }}Copy the code

Context class

public class Context { private Strategy strategy; Public Context(Strategy Strategy){this. Strategy = Strategy; } public void setStrategy(Strategy strategy){ this.strategy = strategy; } public void operate(){ this.strategy.operate(); }}Copy the code

Here’s how it works

public class Zhaoyun { public static void main(String[] args) { Context context; System. Out. Println (" -- -- -- -- -- -- -- -- -- -- just to wu use first guide -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "); context = new Context(new PlanOne()); context.operate(); System.out.println("\n"); System. Out. Println (" -- -- -- -- -- -- -- -- -- -- liu bei happy-chappy use the second guide -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "); context.setStrategy(new PlanTwo()); context.operate(); System.out.println("\n"); System. Out. Println (" -- -- -- -- -- -- -- -- -- -- to chasing behind the sun quan, use the third guide -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "); context.setStrategy(new PlanThree()); context.operate(); System.out.println("\n"); }}Copy the code

六四屠杀

****PS: on these three moves, make the Zhou Lang is “lost the wife and fold soldiers” ah! This is the policy pattern, high cohesion and low coupling, and extensibility. The policy class can be added further, a fourth, a fifth, ha ha

六四屠杀

Fourthly, advantages and disadvantages of strategic mode

Advantages:

  1. The policy pattern provides a way to manage related algorithm families. The hierarchical structure of a policy class defines an algorithm or behavior family. Proper use of inheritance can avoid duplicate code by moving common code into a parent class.

  2. Strong expansibility, open and close principle

  3. You can eliminate a lot of if else in your code, because if else is hard coded and not easy to maintain

  4. An algorithm reuse mechanism is provided. Because the algorithm is isolated and encapsulated in the policy class, different environment classes can easily reuse these policy classes.

  5. You do not want the client to know the complex data structure related to the algorithm. Encapsulating the algorithm and related data structure in a specific policy class can improve the confidentiality and security of the algorithm. ****PS: to tell the truth, this place small Mu also did not understand, after all, the client can choose to use a policy class, that he mouse into this class to see the source code is not on the line, but also talk about what confidentiality and security?? Right!!* * * ** * * ** * * * * * * *)

Disadvantages:

  1. The client must know all the policy classes and decide which one to use. This means that the client must understand the differences between these algorithms in order to choose the right one at the right time. In other words, the policy pattern only works if the client knows all the algorithms or behaviors.

  2. The policy pattern will cause the system to produce many concrete policy classes, and any small change will cause the system to add a new concrete policy class (PS:There areThere are as many if else cases as there are concrete policy classes to create), the problem of too much policy inflation may occur.

  3. Multiple policy classes cannot be used on the client at the same time. That is, the client can use only one policy class at a time when using the policy mode. You cannot use one policy class to complete some functions and then use another policy class to complete other functions. (PS: : For example, if the travel mode is taken as a strategic structure, users can choose to take a taxi or a bus, but they cannot take a taxi or a bus in one part of the way.)

Ok, this is the end of today’s content, walk a hundred miles of half 90, please don’t give up halfway, ok?

Historical tweets

“Thread pool” is not difficult at all!

Interviewer: What are the ways to implement reverse linked lists?

“Fast and slow pointer method” “some SAO operation

It’s 2021, and you still don’t know the solution to the idempotence problem?

Why did Spring transactions fail? Did you step on a pit?

Did you get wrapped up again today? How about “lie flat”?

What’s the difference between Eureka and Zookeeper?

I heard you don’t know what CAP theory is?

TCP three handshakes, four waves hard? NO

Odd “win” techniques for bit operations