Design pattern notes 23 - strategy pattern (strategy)

To write a duck project, the specific requirements are as follows:

1) There are various ducks (such as wild ducks, Peking ducks, teals, etc. Ducks have various behaviors, such as calling, flying, etc.)

2) Display duck information

 

Traditional solution:

All ducks integrated from Duck

 

 

 

Problem analysis and solutions implemented in traditional ways

1) Other ducks all inherit the Duck class, so fly makes all subclasses fly, which is incorrect

2) The problem mentioned above in 1 is actually a problem caused by inheritance: partial changes to classes, especially superclass changes, will affect other parts. there will be spillover effects

3) In order to improve problem 1, we can solve it by overriding the fly method => overriding solution

4) Here comes the problem again, if we have a toy duck ToyDuck, we need ToyDuck to cover all the implementation methods of Duck => solution strategy mode (strategy pattern)

 

 

basic introduction

1) In the strategy pattern (Strategy Pattern), define the algorithm family and encapsulate them separately so that they can be replaced with each other. This mode makes the algorithm change independent of the client who uses the algorithm

2) This algorithm embodies several design principles. First, it separates the changing code from the unchanged code; second, it targets interface programming instead of specific classes (defining the strategy interface); third, multi-use combination/ Aggregation, less inheritance (clients use strategies through composition).

 

 

Principle class diagram of strategy pattern

Explanation: As can be seen from the above figure, the customer context has a member variable strategy or other strategy interfaces. As for which strategy needs to be used, we can specify it in the constructor.

 

Core code:


In main method:
        Duck toyDuck = new ToyDuck();
        toyDuck.fly();

When calling the fly() method:
    public void fly() {         //improved         if(flyBehavior != null) {             flyBehavior.fly();         }     }
        





 

The flyBehavior encapsulates a specific flight strategy, which is set when the flyBehavior is initialized.

 

Notes and Details of the Strategy Pattern

1) The key to the strategy pattern is to analyze the changing and unchanged parts of the project

2) The core idea of ​​the strategy pattern is: use more combination/aggregation and less inheritance; use behavior class combination instead of behavior inheritance. more flexible

3) It embodies the principle of "closed for modification, open for extension". The client does not need to modify the original code to add behavior, just add a strategy (or behavior), avoiding the use of multiple transfer statements (if..else if. .else)

4) Provides a way to replace the inheritance relationship: Strategy mode encapsulates the algorithm in an independent Strategy class so that you can change it independently of its Context, making it easy to switch, easy to understand, and easy to expand

5) It should be noted that every time a strategy is added, a class must be added. When there are too many strategies, the number of classes will be huge

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_22059611/article/details/103315214