Java Design Pattern Series (13) Template method
The template method pattern is the behavior pattern of the class. Prepare an abstract class, implement some of the logic in the form of concrete methods and concrete constructors, and then declare some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the intent of the template method pattern.
I. Structure of the template method
AbstractClass
: abstract class. Used to define the algorithm skeleton and primitive operations, and the specific subclasses implement each step of an algorithm by redefining these primitive operations. In this class, you can also provide a generic implementation of the algorithm.
ConcreteClass
: a concrete implementation class. Used to implement some of the steps in the skeleton of an algorithm to accomplish the functions associated with a particular subclass.
Source
(1) abstract Template role Class
/** * Define Template method, Abstract classes such as primitive operations */ public abstract class abstracttemplate {/** primitive operation 1, the so-called primitive operation is an abstract operation, it must be provided by subclasses to implement */ public abstract void (); /** primitive operation 2 */ public abstract void doprimitiveoperation2 (); /** template method, define algorithm skeleton */ public final void templatemethod () {doprimitiveoperation1 (); doprimitiveoperation2 (); }}
(2) Specific template role classes
/** * 具体实现类,实现原语操作 */publicclassextends AbstractTemplate { publicvoiddoPrimitiveOperation1() { //具体的实现 } publicvoiddoPrimitiveOperation2() { //具体的实现 }}
The key to template mode is that subclasses can displace the mutable parts of the parent class, but subclasses cannot change the top-level logic represented by the template method.
Whenever a new subclass is defined, do not think in terms of the flow of control, but in accordance with the "responsibility" thinking. In other words, you should consider which operations must be replaced, which operations can be displaced, and which actions are not replaceable. Using template mode can make these responsibilities clear.
Second, summary
The methods in the template method can be divided into two main categories: Template method and Basic method.
(1) Template method
A template method is defined in an abstract class that combines basic operational methods to form a total algorithm or a method of total behavior.
An abstract class can have any number of template methods, not limited to one. Each template method can invoke any number of specific methods.
(2) Basic methods
The basic method can be divided into three kinds: abstract method, Concrete method (concrete methods) and hook method.
Abstract method: An abstract method is declared by an abstract class and implemented by a specific subclass. In the Java language, abstract methods are indicated by the Abstraction keyword.
Concrete method: A concrete method is declared and implemented by an abstract class, and the subclass is not implemented or replaced.
Hook method: A hook method is declared and implemented by an abstract class, and subclasses are extended. Usually the implementation given by an abstract class is an empty implementation, as the default implementation of the method.
Record a little bit every day. Content may not be important, but habits are important!
Java Design Pattern Series (13) Template method