Strategy and template method
Strategy encapsulates differentAlgorithmThese algorithms can be replaced with each other to achieve different purposes.
Template Method defines the skeleton of an algorithm in an operation and delays the execution of some steps to its subclass.
Strategy is implemented through object combination.
The template method is implemented through Object Inheritance.
According to the principle of "Combination prevails over inheritance", in most cases, we choose strategy. But it's not the beginning
You know you want to use strategy.
Sometimes it is through micro-refactoring (the combination method is often used) to obtain the template method. when the problem is complicated, the subclass needs to be rewritten.
When many methods of the parent class are inherited from the method that is not required by the subclass, the template method should be
Refactored into strategy. It is like the flying duck mentioned in Header first deign patterns.
Strategy solves the coupling problem caused by inheritance.