What are strategic patterns

As I understand it, the policy pattern is a means to optimize the large number of if-else in the code, essentially taking advantage of Java polymorphism. Advantages, make the code more readable, more hierarchical. Disadvantages, the cost of understanding is relatively large.

Logic to implement the policy pattern

You first need a top-level interface that defines the methods you need to implement. Secondly, it needs to define different classes according to business requirements to realize the interface and realize its own methods. Finally, it needs a container to carry the interface and realize the specific invocation of the method by constructing different parameters of the method.

Code implementation

Simply define a top-level interface and define the run method

Define three implementations, each defining its own method

Define a context and pass the carStratege interface

The final result is as shown in the picture above