Abstract Class using the Template Design Pattern
The Template Method Design Pattern is a behavioral design pattern that defines the program skeleton in an algorithm in a method, but delays some steps to subclasses. It allows subclasses to redefine certain steps of an algorithm without changing the algorithm's structure.
Basic Idea
The basic idea behind the Template Method pattern is to define an algorithm's outline in a base class, but let subclasses implement individual steps. This allows you to reuse the common parts of the algorithm, while providing flexibility to customize specific steps as needed.
Components
Abstract Class: This class defines the template method and declares the primitive methods (steps) that concrete classes must implement.
Concrete Classes: These classes implement the primitive methods declared in the abstract class.
Template Method: This is a method in the abstract class, which contains a series of method calls that every subclass object will follow.
When to Use
When you have common behavior among classes with some steps requiring different implementations.
When you want to avoid code duplication, pulling the common code into a single place and then calling into subclass-specific implementations for the varying behavior.
Advantages
Code Reusability: Common code is factored out, reducing duplication.
Maintainability: Changes to the common algorithm can be made in one place, making the code easier to maintain.
Flexibility: Subclasses can redefine certain steps without altering the structure of the algorithm.
Example
Let's consider a simple example of making different types of coffee and tea.
// Abstract Class
public abstract class CaffeineBeverage {
// Template Method
// We have made this final because we do not want it overridden
public final void prepareRecipe() {
boilWater();
brew();
pourInCup();
addCondiments();
}
public void boilWater() {
System.out.println("Boiling water");
}
public void pourInCup() {
System.out.println("Pouring into cup");
}
// Abstract methods to be implemented by subclasses
public abstract void brew();
public abstract void addCondiments();
}
// Concrete Class
public class Coffee extends CaffeineBeverage {
public void brew() {
System.out.println("Dripping coffee through filter");
}
public void addCondiments() {
System.out.println("Adding sugar and milk");
}
}
// Concrete Class
public class Tea extends CaffeineBeverage {
public void brew() {
System.out.println("Steeping the tea");
}
public void addCondiments() {
System.out.println("Adding lemon");
}
}
In this example, CaffeineBeverage
is the abstract class that defines the template method prepareRecipe()
. This method outlines the steps for making a caffeine beverage. The brew()
and addCondiments()
methods are left abstract, allowing subclasses like Coffee
and Tea
to provide their own implementations.
The Template Method pattern allows you to define the "skeleton" of an algorithm in the base class but lets subclasses override specific steps without changing the overall structure and flow of the algorithm.
COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark