Template Method for Java Development – Tutorial Alert

You will learn from the basic about Template method from Java development India based experts in this story. The code sample used by developers is for reference purpose only. You will learn how Template Method makes the code more reusable and prevents duplicating of the code. Read further for more information.

Introduction

In Java, to make the code more reliable we should design our code efficiently that can be done by applying design methodologies such as OOPs, OOAD, design principles and patterns into applications.

GOF says that – “Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithms structure.”

Template Method for Java Development – Tutorial Alert

Template Method is a behavioural design pattern which is used where there are two or more similar behaviour or implementation exists for an algorithm.

In a real world template method is applicable at all the places,

For example,

  • Playing a video game always having the steps – 1. Init, 2. Play, 3. Destroy but the games can be different like cricket, football, volley ball etc.,
  • Transporting things always having the steps – 1. Init or Place Order 2. Load 3. Tracking 3. Deliver but the transports can be different like torus, tailor (vary in load capacity and speed)
  • Constructing a building or house always having the steps – 1. Design 2. Build Base 3. Make Pillars 4. Construct wall 5. Furnish but the buildings can be different by design, base, pillars and wall (brick wall or glass wall or wood wall).

In such scenarios, we can have a specific algorithm like Building which may have the steps – 1. Design 2. Build Base 3. Make Pillars 4. Construct wall 5. Furnish and the sub classes that are different kind of buildings Office, Hotel, Shopping Mall, Temple, House etc. which will take care of each steps but will follows the same algorithm

e.g.

public abstract class Building {

// template method

// a final method contains all the steps to be executed in an algorithm

public final construct() {

design();

buildBase();

makePillars();

constructWall();

furnish();

}

// steps are declared as abstract methods and the behaviour will be implemented in sub classes

protected abstract void design();

protected abstract void buildBase();

protected abstract void makePillars ();

protected abstract void constructWall();

protected abstract void furnish();

}

// Office is a building which may vary in design, base, pillars, wall and furnishing and it will be implemented or overridden as requires for an office

public class Office extends Building {

protected abstract void design() {

// design for office (official)

}

protected abstract void buildBase() {

// base for office

}

protected abstract void makePillars() {

// pillars for office

}

protected abstract void constructWall() {

// wall for office (glass walls may be)

}

protected abstract void furnish() {

// furnishing for office

}

}

// Hotel is a building which may vary in design, base, pillars, wall and furnishing and it will be implemented or overridden as requires for a hotel

public class Hotel extends Building {

protected abstract void design() {

// design for hotel (commercial)

}

protected abstract void buildBase() {

// base for hotel

}

protected abstract void makePillars() {

// pillars for hotel

}

protected abstract void constructWall() {

// wall for hotel

}

protected abstract void furnish() {

// furnishing for hotel

}

}

// House is a building which may vary in design, base, pillars, wall and furnishing and it will be implemented or overridden as requires for an house

public class House extends Building {

protected abstract void design() {

// design for house (personal)

}

protected abstract void buildBase() {

// base for house

}

protected abstract void makePillars() {

// pillars for house

}

protected abstract void constructWall() {

// wall for house

}

protected abstract void furnish() {

// furnishing for house

}

}

public class Client {

public static void main(String aa[]) {

//construct hotel

Hotel hotel = new Hotel();

hotel.construct();

//construct office

Office office = new Office();

office.construct();

//construct house

House house = new House();

house.construct();

}

}

Conclusion

Just think that if we didn’t use template method how would we construct these hotel, house and office? On each class hotel, house and office we would be created a duplicate method called construct!!

Template Method makes our code more reusable and it actually avoids duplicating the code. So do apply template method pattern wherever the scenario (where two or more behaviours exists for an algorithm) appears. For doubts or query, make comments below.

Author Bio:

This tutorial is created and shared by Ethan Millar who is a Java development India based experts tow write technical articles. This post to make you learn about the Template Method. You can use this method and make your code more reusable and prevent duplication of code just like professionals.

Leave a Reply

Your email address will not be published. Required fields are marked *