Template method pattern, is a very simple design pattern, we may often use in the real development, but we do not realize that this is a design pattern. First, consider the definition of the template method pattern:
Define The skeleton of an algorithm in a operation,deferring some steps to Subclasses,template Method lets subclasses red Efine certain steps of an algorithm without changing the algorithm ' s structure. (Define an algorithmic framework in an operation, and defer some steps into subclasses.) Enables subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm. )
Class diagram for Template method pattern:
Look at the class diagram to know how simple, just use the inheritance mechanism. Here's a look at common code implementations:
Public abstract class abstracttemplatemethod{
//Basic method
protected abstract void doSomething1 ();
The basic method
protected abstract void doSomething2 ();
Template method public
final void Templatemethod () {
//Call the basic method, complete the corresponding logic
this.dosomething1 () as required;
This.dosomething2 ();
}
}
Concrete Template method Implementation class:
public class TemplateMethod1 extends abstracttemplatemethod1{
//Basic method
protected void DoSomething1 () {
// Logic code
...
}
Basic method
protected void DoSomething2 () {
//Logical Code
...
}
}
Specific implementation of what kind of template method is required, according to the requirements of the specific implementation, the specific call code:
public class Testmain {public
static void main () {
new TemplateMethod1 (). Templatemethod ();//will execute according to the logic implemented in the specific subclass
}
}
The basic method in an abstract template is best designed to be a protected type, without exposing properties or methods as much as possible to the private type:
is not so easy. Take a look at the pros and cons of template method patterns.
Advantages:
1. Package The invariant part, expand the variable part;
2. Extract the public part code, easy to maintain;
3. Behavior is controlled by the parent class, and the subclass is responsible for implementation.
Disadvantages:
According to the custom, the abstract class is responsible for declaring the most abstract, the most general things properties and methods, implementation of the class to complete specific things properties and methods, but the template method pattern is reversed, the abstract class defines a partial abstract method, subclasses implementation, the result of the subclass execution affects the result of the parent class, that is, the child class has an impact Increases the difficulty of code reading.
Template method Pattern Usage scenarios:
A hook method is a method that returns a Boolean value in an abstract class to influence the execution result of a template method by judging it.
For example:
Public abstract class abstracttemplatemethod{
//Basic method
protected abstract void doSomething1 ();
The basic method
protected abstract void doSomething2 ();
The hook method
protected Abstract Boolean isflag ();/
/Template method public
final void Templatemethod () {
//Call the Basic method, Complete the corresponding logic
this.dosomething1 () according to the requirement;
if (Isflag ()) { //affects the execution result of the template method <pre name= "code" class= "java" > this.dosomething2 ();
} }}
Here's a question: what should we do when we want to control the order of the basic methods in the template method. (This can add a list to define the order of execution)
Important: I use the void modifier in all design pattern generic code to modify the function, not to represent that the function cannot be returned with a value ... This method is to see the specific needs, specific scenarios to achieve.