This is the 15th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
preface
Suppose we need to build a house, the steps are: foundation -> wall -> roof.
Our requirement is to build a cement house and a wood house, so the corresponding steps may need to deal with different logic, but the execution steps are fixed, so the fixed execution steps can be defined as a method template.
Template method definition
The template approach is a behavior design pattern. The template method design pattern is used to create method execution templates and defer some implementation steps to subclasses.
A template method defines the steps that a method performs, and it can provide a default implementation common to all or some subclasses.
Template method superclass
To complete the previous example, the steps needed to build a house are: the foundation, the walls, and the roof. The important thing is, we can’t change the order of execution, because we can’t build the roof before we build the foundation. In this case, we can create a template method that uses a different method to build the house.
Now, the foundation for building a house is the same for all types of houses, whether wooden or concrete. We can provide the base implementation, or override this method if subclasses want to, but for the most part it’s universal for all types of houses.
/ * * *@authorHei says Java *@ClassName HouseBuildTemplate
* @DescriptionBuilding templates *@date2021/11/14 * * /
public abstract class HouseTemplate {
// A template for building a house
public final void buildHouse(a) {
// 1. Build the foundation
buildFoundation();
/ / 2. The wall of build by laying bricks or stones
buildWalls();
// 3. Build the roof
buildRoof();
System.out.println("The house is finished.");
}
private void buildFoundation(a) {
System.out.println("Build the foundation.");
}
protected abstract void buildWalls(a);
protected abstract void buildRoof(a) ;
}
Copy the code
To ensure that subclasses do not override template methods, buildHouse() should be set to final.
Template method implementation class
Since we need to build wood houses and concrete houses, some methods need to be implemented by subclasses, so we must make the base class abstract.
The wood house
public class WoodHouse extends HouseTemplate {
@Override
protected void buildWalls(a) {
System.out.println("Build a wall out of wood.");
}
@Override
protected void buildRoof(a) {
System.out.println("Build a wooden roof."); }}Copy the code
Cement room
/ * * *@authorHei says Java *@ClassName CementHouse
* @DescriptionCement room *@date2021/11/14 * * /
public class CementHouse extends HouseTemplate {
@Override
protected void buildWalls(a) {
System.out.println("Build cement walls.");
}
@Override
protected void buildRoof(a) {
System.out.println("Build a cement roof."); }}Copy the code
Template method client
Let’s test the template method pattern example with a test program.
/ * * *@authorHei says Java *@ClassName TemplateMethodTest
* @date2021/11/14 * * /
public class TemplateMethodTest {
public static void main(String[] args) {
// Use the template method
WoodHouse woodHouse = new WoodHouse();
woodHouse.buildHouse();
System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
CementHouse cementHouse = newCementHouse(); cementHouse.buildHouse(); }}Copy the code
Notice that the client is calling the template methods of the base class, using some methods from the base class and some methods from subclasses, depending on the implementation step.
Running results:
Template method class diagram
The template method pattern in the JDK
- Non-abstract methods in java.io.InputStream, java.io.OutputStream, java.io.Reader, java.io.
- Non-abstract methods in java.util.AbstractList, java.util.AbstractSet, java.util.AbstractMap.
Template method design pattern essentials
- Template methods are made up of fixed steps. Template methods should be final. For some steps, base and subclass implementations can be different;
- Most of the time, a subclass calls a method in its parent class, but in the template method pattern, a superclass template method calls a method in a subclass;
- The default implementation methods of the base class are called hooks. They are intended to be overridden by subclasses, and if you want methods that are not overridden, you can modify them to final.
That’s all about the template method mode. If it’s helpful, give it a thumbs up!