The source code is JDK1.8 as reference
1. Definition:
Defines the framework for an algorithm in an operation, and delays some steps into subclasses. Enables subclasses to redefine some specific steps of the algorithm without changing the mechanism of an algorithm.
2. Analysis:
General class Diagram:
class Diagram parsing:
The template method pattern is very simple and only uses the Java inheritance mechanism, but it is a very wide application pattern.
2.1. Abstract template (AbstractClass)
It mainly defines some basic operations (methods) in the template, and its methods are divided into two categories:
Basic methods:
The basic method is also called the basic operation, which is the method implemented by the subclass and is called by the template method.
Template method:
Can be by one or several, is generally a concrete method, that is, a framework to implement the basic method of scheduling, complete the fixed logic.
2.2. Specific templates (ConcreteClass1 and ConcreteClass2)
Implements one or more abstract methods defined by the parent class, that is, the basic method of the parent class definition is implemented in the subclass.
3. Specific application:
3.1. Abstract template (AbstractClass)
Public Abstract classAbstractClass {//: Basic method protected Abstract void dosomething();//: Basic method protected Abstract void doanything();//: Template Method Public void Templatemethod(){/ * * Call the Basic method to complete the relevant logic * / This. doanything (); This. dosomething (); }}
3.2. Specific templates (ConcreteClass1 and ConcreteClass2)
Public class ConcreteClass1 extends abstractclass { //: Implementing the Basic Method protected void doanything() {//: Business logic processing}protected void dosomething() {//: Business logic processing}} Public class ConcreteClass2 extends abstractclass { //: Implementing the Basic Method protected void doanything() {//: Business logic processing}protected void dosomething() {//: Business logic processing}}
3.3. Scene class:
publicclass Client { publicstaticvoidmain(String[] args) { new ConcreteClass1(); new ConcreteClass2(); // .. 调用模板方法 class1.templateMethod(); class2.templateMethod(); }}
The above completed a simple template method design and application, in fact, the principle is very simple, abstract the parent class defined the basic method and template method, the implementation of the template methods of the specific logic, concrete template sub-class implementation of the basic method of the abstract template.
In fact, in the JDK source code or open source framework, a lot of the use of this design pattern, such as the implementation of Java.io.InputStream, as follows:
Public Abstract class InputStream implements closeable {... Public Abstract int Read()throwsIOException; Public int Read(byteB[])throwsIOException {returnRead (b,0, b.length); } Public int Read(byteB[],intOffintLenthrowsIOException {if(b = =NULL) {Throw NewNullPointerException (); }Else if(Off <0|| Len <0|| Len > B.length-off) {Throw NewIndexoutofboundsexception (); }Else if(len = =0) {return 0; }intc = Read ();if(c = =-1) {return-1; } B[off] = (byteCinti =1;Try{ for(; i < Len; i++) {c = read ();if(c = =-1) { Break; } B[off + i] = (byteC } }Catch(IOException ee) { }returnI } ...}
In the Java.io.InputStream class, the template method public int read (byte b[], int off, int len) implements the specific business logic, while the basic method public abstract int read () is implemented by subclasses, and is called in the template method, thus implements the operation that all subclasses under Java.io.InputStream can complete the template method. In fact, in the open source framework of the source code, it is easy to see similar design.
4. Application Scenario:
Can you remember the four characteristics of object-oriented: encapsulation, inheritance, polymorphism, abstraction. The Template method design pattern is to take full advantage of the characteristics of the face object, highly abstract class and class behavior, the public part to the abstract parent class to implement, the behavior is controlled by the parent class, subclass implementation. This type of design can easily be extended, making it easier to maintain in the later stages.
Application Scenarios:
• Multiple subclasses have public methods, and the logic is essentially the same.
• Important, complex algorithms that can be designed as template methods, and the surrounding detail functions are implemented by each sub-class.
• When refactoring, the template method pattern is a frequently used pattern that extracts the same code into the parent class and then constrains its behavior through the hook function
5. Summary:
The template method design pattern is a very common design pattern in everyday life, perhaps you have not heard the name of the template method design pattern, but have been exposed to this design pattern, when designing a class or structure, A highly abstract pattern that can be highly abstract between class commonalities and complete a good design.
Note: I am referring to the "design mode of Zen" and "design mode" two books learned, which added their own understanding of iterator design patterns, as well as the JDK in the source code understanding.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Template method pattern for Java design pattern (iv)