Design Pattern _ template method pattern
Template Method Pattern
Define the skeleton of an algorithm in an operation, defering some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
Defines the framework of an algorithm for an operation, and delays some steps to the subclass. This allows subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.
Template
Public abstract class AbstractClass {// basic method protected abstract void doSomething (); // basic method protected abstract void doAnything (); // template method public void templateMethod () {/** call the basic method to complete the related logic */this. doAnything (); this. doSomething ();}}
Advantages encapsulation unchanged, scalable variable. Extract some common code for easy maintenance. The behavior is controlled by the parent class and implemented by the Child class.
There are multiple sub-classes in the application scenario. The logic is basically the same as the important and complex algorithms. The core algorithms are designed as templates, and the details are implemented by sub-classes.