Template Method pattern is used to implement a process where subclasses can change some steps of the parent.
This article demonstrates Template Method pattern implementations in Java. Check the following examples.
Examples
Here are examples of Templete Method Pattern implementation in Java-
Example #1: Transport Construction Template
Let’s consider an example of a transport builder.
Transport Abstract Class
// Transport.java
package com.bigboxcode.designpattern.templatemethod.transport;
public abstract class Transport {
abstract void createBody();
abstract void addEngine();
abstract void addWheel();
// Required only for Plane
abstract void addWing();
private void addSeat() {
// Add seats to the vehicle
// Adding seats are same for all transports so same functions for all
System.out.println("Adding seats");
}
private void paint() {
System.out.println("Painting");
}
public void build() {
createBody();
addEngine();
addWheel();
addWing();
addSeat();
paint();
}
}
Car Transport Class
// Car.java
package com.bigboxcode.designpattern.templatemethod.transport;
public class Car extends Transport {
@Override
void createBody() {
System.out.println("Creating Car Body");
}
@Override
void addEngine() {
System.out.println("Adding Engine to Car");
}
@Override
void addWheel() {
System.out.println("Adding 4 Wheels to Car");
}
@Override
void addWing() {
// Not required for Car
}
}
Bike Transport Class
// Bike.java
package com.bigboxcode.designpattern.templatemethod.transport;
public class Bike extends Transport {
@Override
void createBody() {
System.out.println("Creating Bike Body");
}
@Override
void addEngine() {
System.out.println("Adding Engine to Bike");
}
@Override
void addWheel() {
System.out.println("Adding 2 Wheels to Bike");
}
@Override
void addWing() {
// Not required for Bike
}
}
Plane Transport Class
// Plane.java
package com.bigboxcode.designpattern.templatemethod.transport;
public class Plane extends Transport {
@Override
void createBody() {
System.out.println("Creating Plane Body");
}
@Override
void addEngine() {
System.out.println("Adding Engine to Plane");
}
@Override
void addWheel() {
System.out.println("Adding 3 Wheels to Plane");
}
@Override
void addWing() {
System.out.println("Adding Wings Plane");
}
}
Train Transport Class
// Train.java
package com.bigboxcode.designpattern.templatemethod.transport;
public class Train extends Transport {
@Override
void createBody() {
System.out.println("Creating Train Body");
}
@Override
void addEngine() {
System.out.println("Adding Engine to Train");
}
@Override
void addWheel() {
System.out.println("Adding Wheels to Train");
}
@Override
void addWing() {
// Not required for Train
}
}
Demo
package com.bigboxcode.designpattern.templatemethod.transport;
public class Demo {
public static void main(String[] args) {
Transport car = new Car();
car.build();
Transport plane = new Plane();
plane.build();
}
}
Output
Creating Car Body
Adding Engine to Car
Adding 4 Wheels to Car
Adding seats
Painting
Creating Plane Body
Adding Engine to Plane
Adding 3 Wheels to Plane
Adding Wings Plane
Adding seats
Painting
Source Code
Use the following link to get the source code:
Example | Source Code Link |
---|---|
Example #1: Transport Builder | GitHub |
Other Code Implementations
Use the following links to check Template Method pattern implementation in other programming languages.