Template Method for Java Development – Tutorial Alert

You will get an idea from the fundamental Template technique from Java development India-based experts in this story. The code example used by developers is for reference purposes only. You will learn how Template Method builds the code more recyclable and stop duplicating the code. Read further for more information.

 

Introduction

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

 

GOF says that – “Describe the skeleton of an algorithm in a technique, deferring some steps to subclasses. Template Technique lets subclasses reconsider certain steps of an algorithm without changing the construction of the algorithm”.

f:id:aegissofttech:20220111194443p:plain

Template Technique is an observable design that is used where there are two or more same behavior or execution that obtain for an algorithm.

A real-world template technique is appropriate in all places,

For example,

  • Playing a video game always has the steps – 1. Init, 2. Play, 3. Crash but the games can be Changed like cricket, football, volleyball, etc.,
  • Transporting things at all times has the steps – 1. Init or Place Order 2. Load 3. Tracking 3. Deliver but the transports can be changed like torus, tailor (vary in load capacity and speed)
  • Establishing a building or house always has the steps – 1. Design 2. Build Base 3. Make Pillars 4. Construct wall 5. Furnish but the construction can be changed by design, base, pillars, and wall (brick wall or glass wall or wood wall).

In such a structure, we can have a particular algorithm like Building which may have the steps – 1. Design 2. Build Base 3. Make Pillars 4. Construct wall 5. Furnish and the subclasses that are dissimilar kinds of construct Office, Hotel, Shopping Mall, Temple, House, etc. which will take care of all step but will follow 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 that may vary in design, base, pillars, wall, and furnishing and it will be implemented or overridden as required 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 that may vary in design, base, pillars, wall, and furnishing and it will be implemented or overridden as required 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 that may vary in design, base, pillars, wall, and furnishing and it will be implemented or overridden as required for a 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

}

}

Read Also: Be prepared for the Li-Fi technology 100 times faster than traditional Wi-Fi

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 assume that if we didn’t use the template technique how would we build these hotels, houses, and offices? On all class hotels, houses and offices we would create a duplicate technique called construct!!

Template Technique builds our code more reusable and it actually avoids copying the code. So do apply template technique pattern wherever the structure (where two or more behaviors exist for an algorithm) appears. For doubts or queries, make comments below.

Author Bio:

This tutorial is created and shared by Ethan Millar who is a Java development India-based experts tow writes technical articles. This post is 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.