Template Pattern
In the Template Pattern, an abstract class publicly defines the way/template for executing its methods. Its subclasses can override method implementations as needed, but the call will be made in the way defined in the abstract class. This type of design pattern belongs to the behavioral patterns.
Introduction
Intent
Defines the skeleton of an algorithm in a parent class and allows subclasses to redefine certain specific steps of the algorithm without changing its structure.
Main problem solved
- Solves the problem of repeatedly implementing the same methods in multiple subclasses by abstracting common methods into the parent class to avoid code duplication.
Use Cases
- When there are common methods that can be shared among multiple subclasses.
Implementation
- Define the abstract parent class: Contains template methods and some abstract methods or concrete methods.
- Implement subclasses: Inherit the abstract parent class and implement abstract methods without changing the algorithm structure.
Key code
- Template Method: Defined in the abstract parent class, it calls abstract methods and concrete methods.
- Abstract method: Implemented by subclasses, representing the variable parts of the algorithm.
- Concrete methods: Implemented in the abstract parent class, representing the invariant parts of the algorithm.
Application Example
- Building process: Steps such as the foundation, wiring, and plumbing are the same, while later construction steps such as adding closets and fences differ.
- The 81 trials of Journey to the West: The 81 trials set by the Bodhisattvas represent a top-level logical skeleton.
- Spring's support for Hibernate: Encapsulates common methods such as starting transactions, obtaining Session, and closing Session.
Advantages
- Encapsulate the invariant parts: The invariant parts of the algorithm are encapsulated in the parent class.
- Extend the variable parts: Subclasses can extend or modify the variable parts of the algorithm.
- Extract common code: Reduces code duplication and facilitates maintenance.
Disadvantages
- Increased number of classes: Each different implementation requires a subclass, which may make the system large.
Usage recommendation
- Consider using the Template Method Pattern when multiple subclasses share methods with the same logic.
- For important or complex methods, consider defining them as template methods in the parent class.
Notes
- To prevent malicious modification, template methods are usually decorated with the
finalkeyword to avoid being overridden by subclasses.
Main roles included
-
Abstract Class:
- Defines template methods and some abstract methods or concrete methods.
-
Concrete Classes:
- Inherits from the abstract parent class and implements abstract methods.
-
Hook Method (optional):
- Defined in the abstract parent class and can be overridden by subclasses to affect the behavior of the template method.
-
Client (optional):
- Use the abstract parent class and concrete subclasses without worrying about the details of template methods.
Create a class that implements
We will create an abstract class that defines operations, where the Game template method is set to final so that it cannot be overridden.Cricket and Football are concrete classes that extend Game the abstract class and override its methods.
TemplatePatternDemo, and our demo class uses the Game to demonstrate the use of the Template Pattern.
Step 1
Create an abstract class whose template method is set to final.
Game.java
public
abstract
class
Game
{
abstract
void
initialize
(
)
;
abstract
void
startPlay
(
)
;
abstract
void
endPlay
(
)
;
//
Template
public
final
void
play
(
)
{
//
Initialize the game
initialize
(
)
;
//
Start the game
startPlay
(
)
;
//
End the game
endPlay
(
)
;
}
}
Step 2
Create the entity classes that extend the above class.
Cricket.java
public
class
Cricket
extends
Game
{
@
Override
void
endPlay
(
)
{
System
.
out
.
println
(
"
Cricket Game Finished!
"
)
;
}
@
Override
void
initialize
(
)
{
System
.
out
.
println
(
"
Cricket Game Initialized! Start playing.
"
)
;
}
@
Override
void
startPlay
(
)
{
System
.
out
.
println
(
"
Cricket Game Started. Enjoy the game!
"
)
;
}
}
Football.java
public
class
Football
extends
Game
{
@
Override
void
endPlay
(
)
{
System
.
out
.
println
(
"
Football Game Finished!
"
)
;
}
@
Override
void
initialize
(
)
{
System
.
out
.
println
(
"
Football Game Initialized! Start playing.
"
)
;
}
@
Override
void
startPlay
(
)
{
System
.
out
.
println
(
"
Football Game Started. Enjoy the game!
"
)
;
}
}
Step 3
Use Game the template method play() to demonstrate how the game is defined.
TemplatePatternDemo.java
public
class
TemplatePatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
Game
game
=
new
Cricket
(
)
;
game
.
play
(
)
;
System
.
out
.
println
(
)
;
game
=
new
Football
(
)
;
game
.
play
(
)
;
}
}
Step 4
Run the program, output result:
Cricket Game Initialized! Start playing.
Cricket Game Started. Enjoy the game!
Cricket Game Finished!
Football Game Initialized! Start playing.
Football Game Started. Enjoy the game!
Football Game Finished!