Defined
Template method mode
Defines the skeleton of an algorithm in an operation, and completes the implementation of some of the steps in a subclass.
The template method pattern allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.
The template method pattern is one of the most common patterns in all patterns and is the basic technique for code reuse based on inheritance .
Therefore, in the class structure diagram of the template method pattern, there is only an inheritance relationship and no association relationship.
The template method pattern requires collaboration between the designer of the abstract class and the specific subclass. A designer is responsible for giving the outline and skeleton of an algorithm, while others are responsible for the various logical steps of the algorithm.
The method of representing these specific logical steps is called
Basic methods (primitive method)And the method of summing up these basic methods is called
Template Method, the name of the design pattern is from here.//ExampleAbstractclassTemplate {
PublicAbstractvoidOperationa ();
PublicAbstract voidOperationb ();
PublicvoidTemplatemethod () {
Operationb ();
Operationa ();
}
}
classConcreteaextendsTemplate {
@Override
PublicvoidOperationa () {
System.out.println ("Concretea Operationa ()");
}
@Override
PublicvoidOperationb () {
System.out.println ("Concretea operationb ()");
}
}
classConcretebextendsTemplate {
@Override
PublicvoidOperationa () {
System.out.println ("Concreteb Operationa ()");
}
@Override
Public voidOperationb () {
System.out.println ("Concreteb operationb ()");
}
}
PublicclassTemplatemethodpattern {
PublicStaticvoidMain (string[] args) {
Template obj =NULL;
obj =NewConcretea ();
Obj. Templatemethod ();
obj =NewConcreteb ();
Obj. Templatemethod ();
}
}
//Run ResultsConcretea operationb ()
Concretea Operationa ()
Concreteb operationb ()
Concreteb Operationa ()
Points
three types of roles in template method patterns1. Specific methods (concrete method)
2. Abstract method
3. Hook method
Association of three types of roles
In the template method pattern, the parent class first defines the framework of an algorithm, which is all the methods necessary to implement the algorithm.
Where the common code is placed in the specific method of the parent class.
The code for each subclass's particularity is placed in the specific method of the subclass. However, there is a need for a corresponding abstract method declaration in the parent class.
The hook method allows subclasses to decide whether to hook up different points of the algorithm.
Summarize
The template method pattern can be used to extract the common behavior of code for reuse purposes.
The behavior for specialization is implemented in subclasses. The template method of the parent class can control the specific implementation in the subclass.
Subclasses do not need to understand the overall algorithm framework, just implement their own business logic.
Instance
The template method pattern is widely used.
In"Head First"A very representative example is given in the section on template method patterns.
In real life, tea and coffee are ubiquitous drinks. What is the process of brewing a cup of tea or brewing a cup of coffee?
Let's tidy up the process.
Tea: Boiled water ==> brew tea ==> pour into the cup ==> add lemon Brew Coffee: Boiling water ==> brew coffee ==> pour into the cup ==> add sugar and milk |
It is not difficult to find out from the above steps that the process of preparing the two drinks is very similar. We can use the template class method to define the algorithm framework for making drinks.
One of the same common steps (such as boiling water, pouring into the cup), directly in the abstract class to give concrete implementation.
For the step with the difference, the implementation is given in the specific class.
//Abstract classAbstractclassBeverage {
//The template method determines the skeleton of the algorithm. Equivalent to the Templatemethod () method
PublicvoidPreparebeverage () {
Boilwater ();
Brew ();
Pourincup ();
if(Customwantscondiments ())
{
Addcondiments ();
}
}
//generic operations, defined directly in the abstract class
PublicvoidBoilwater () {
System.out.println ("boiled water");
}
//generic operations, defined directly in the abstract class
PublicvoidPourincup () {
System.out.println ("Pour into cup");
}
//Hook method that determines whether certain algorithm steps are hooked up in the algorithm
PublicBooleanCustomwantscondiments () {
return true;
}
//Special Operations, specifically implemented in subclasses
PublicAbstractvoidBrew ();
//Special Operations, specifically implemented in subclasses
PublicAbstractvoidAddcondiments ();
}
//Specific ClassclassTeaextendsBeverage {
@Override
PublicvoidBrew () {
System.out.println ("brewing tea");
}
@Override
PublicvoidAddcondiments () {
System.out.println ("Add lemon");
}
}
classCoffeeextendsBeverage {
@Override
PublicvoidBrew () {
System.out.println ("Brewed coffee beans");
}
@Override
PublicvoidAddcondiments () {
System.out.println ("Add sugar and milk");
}
}
//Test Code PublicStaticvoidMain (string[] args) {
SYSTEM.OUT.PRINTLN ("============= preparation tea =============");
Beverage Tea =NewTea ();
Tea.preparebeverage ();
System.out.println ("============= Prepare coffee =============");
Beverage Coffee =NewCoffee ();
Coffee.preparebeverage ();
}
//Run Results
============= Prepare tea =============
Boil boiling water
Brewing tea
Pour into the cup
Add lemon
============= Preparing Coffee =============
Boil boiling water
Brewing coffee beans
Pour into the cup
Add sugar and milk
JAVA design Pattern Template Method pattern