Core Java Design Pattern : Behavioral Pattern : Template Method Design Pattern
Template Method Design Pattern |
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
The Template Method pattern should be used
- to implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary.
- when common behavior among subclasses should be factored and localized in a common class to avoid code duplication. This is a good example of “refactoring to generalize” as described by Opdyke and Johnson. You first identify the differences in the existing code and then separate the differences into new operations. Finally, you replace the differing code with a template method that calls one of these new operations.
- to control subclasses extensions. You can define a template method that calls “hook” operations (see Consequences) at specific points, thereby permitting extensions only at those points.
The Template Method defines a skeleton of an algorithm in an operation, and defers some steps to subclasses. Home builders use the Template Method when developing a new subdivision. A typical subdivision consists of a limited number of floor plans with different variations available for each. Within a floor plan, the foundation, framing, plumbing, and wiring will be identical for each house. Variation is introduced in the later stages of construction to produce a wider variety of models.
AbstractClass (Application)
- defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
- implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
ConcreteClass (MyApplication)
- implements the primitive operations to carry out subclass-specific steps of the algorithm.
Examples |
RobotTemplate.java
package com.javaskool.template;
public abstract class RobotTemplate {
public final void go() {
start();
getParts();
assemble();
test();
stop();
}
public void start() {
System.out.println("Starting....");
}
public void getParts() {
System.out.println("Getting parts....");
}
public void assemble() {
System.out.println("Assembling....");
}
public void test() {
System.out.println("Testing....");
}
public void stop() {
System.out.println("Stopping....");
}
}
AutomotiveRobot.java
package com.javaskool.customized;
import com.javaskool.template.RobotTemplate;
public class AutomotiveRobot extends RobotTemplate {
private String name;
public AutomotiveRobot(String n) {
name = n;
}
public void getParts() {
System.out.println("Getting a carburetor....");
}
public void assemble() {
System.out.println("Installing the carburetor....");
}
public void test() {
System.out.println("Revolving the engine....");
}
public String getName() {
return name;
}
}
CookieRobot.java
package com.javaskool.customized;
import com.javaskool.template.RobotTemplate;
public class CookieRobot extends RobotTemplate {
private String name;
public CookieRobot(String n) {
name = n;
}
public void getParts() {
System.out.println("Getting flour and sugar....");
}
public void assemble() {
System.out.println("Baking a cookie....");
}
public void test() {
System.out.println("Crunching a cookie....");
}
public String getName() {
return name;
}
}
TestDrive.java
package com.javaskool;
import com.javaskool.customized.AutomotiveRobot;
import com.javaskool.customized.CookieRobot;
public class TestDrive {
public static void main(String args[]) {
AutomotiveRobot automotiveRobot = new AutomotiveRobot("Automotive Robot");
CookieRobot cookieRobot = new CookieRobot("Cookie Robot");
System.out.println(automotiveRobot.getName() + ":");
automotiveRobot.go();
System.out.println();
System.out.println(cookieRobot.getName() + ":");
cookieRobot.go();
}
}
Output
c:\>javac -d . *.java
c:\>java com.javaskool.TestDrive
Automotive Robot:
Starting....
Getting a carburetor....
Installing the carburetor....
Revolving the engine....
Stopping....
Cookie Robot:
Starting....
Getting flour and sugar....
Baking a cookie....
Crunching a cookie....
Stopping....
Recent Comments