Monday, 9 December 2013

Template Design pattern or Template Method Design pattern - Implementation [Coffee]

Enjoying Java & Design Pattern Tutorials?

Subscribe to Ram N Java for simplified programming guides, architecture patterns, and practical code walkthroughs!

🔔 Subscribe to Ram N Java

What is the Template Method Design Pattern?

The Template Method Pattern is a behavioral design pattern that defines the skeleton of an algorithm in an abstract base class, allowing subclasses to redefine certain steps without altering the overall structure.

Think of making coffee: every coffee recipe follows a standard sequence — boiling water, brewing/adding coffee powder, adding milk, and adding sugar. While the steps are identical, the exact ingredients and quantities differ depending on whether you are preparing Bru Coffee or Nescafé.

The Real-World Coffee Preparation Analogy

In our example, we define an abstract class called CoffeeTemplate containing:

  • Abstract Methods: boilWater(), addMilk(), addSugar(), and addCoffeePowder(). Subclasses implement these specific steps.
  • Template Method: prepareCoffee(). This method executes the sequence of steps and is marked final so subclasses cannot alter the overall algorithm.

Java Implementation Breakdown

Here is how the core structure is laid out in code:

// Abstract Template Class
public abstract class CoffeeTemplate {

    // Template method defined as final
    public final void prepareCoffee() {
        boilWater();
        addMilk();
        addSugar();
        addCoffeePowder();
        System.out.println("Coffee is ready!\n");
    }

    public abstract void boilWater();
    public abstract void addMilk();
    public abstract void addSugar();
    public abstract void addCoffeePowder();
}

The concrete classes BruCoffee and NescafeCoffee extend CoffeeTemplate and specify the exact quantities and powder types. The client simply invokes prepareCoffee(), maintaining clean separation of concerns and eliminating code duplication.

Related Tutorials from Ram N Java


Click here to watch in Youtube : https://www.youtube.com/watch?v=q02WKTGTW_0

Click the below Image to Enlarge
Template Design pattern or Template Method Design pattern - Implementation [Coffee]




Template Design pattern or Template Method Design pattern - Implementation [Coffee] - Class Diagram







































CoffeeTemplate.java

public abstract class CoffeeTemplate
{
/*
 *  prepareCoffee method is a template method,this method is final,so
 *  subclasses cannot override.
 */
public final void prepareCoffee()
{
boilWater();
addMilk();
addSugar();
addCoffeePowder();
System.out.println("Hot coffee ready.....");
}

public abstract void boilWater();

public abstract void addMilk();

public abstract void addSugar();

public abstract void addCoffeePowder();
}

BruCoffee.java

public class BruCoffee extends CoffeeTemplate
{

@Override
public void boilWater()
{
System.out.println("100 ml water boiled");

}

@Override
public void addMilk()
{
System.out.println("100 ml milk Added");

}

@Override
public void addSugar()
{
System.out.println("100 grams sugar Added");

}

@Override
public void addCoffeePowder()
{
System.out.println("10 grams Bru coffee powder Added");

}

}

NescafeCoffee.java

public class NescafeCoffee extends CoffeeTemplate
{

@Override
public void boilWater()
{
System.out.println("200 ml water boiled");

}

@Override
public void addMilk()
{
System.out.println("200 ml milk Added");

}

@Override
public void addSugar()
{
System.out.println("200 grams sugar Added");

}

@Override
public void addCoffeePowder()
{
System.out.println("20 grams Nescafe coffee powder Added");

}

}

CoffeeClient.java

public class CoffeeClient
{

public static void main( String[] args )
{

System.out.println("Bru coffee preparation\n");
CoffeeTemplate bruCoffee = new BruCoffee();
bruCoffee.prepareCoffee();

System.out.println("*******************************************");

System.out.println("Nescafe coffee preparation\n");
CoffeeTemplate NescafeCoffee = new NescafeCoffee();
NescafeCoffee.prepareCoffee();

}

}

Output

Bru coffee preparation

100 ml water boiled
100 ml milk Added
100 grams sugar Added
10 grams Bru coffee powder Added
Hot coffee ready.....
*******************************************
Nescafe coffee preparation

200 ml water boiled
200 ml milk Added
200 grams sugar Added
20 grams Nescafe coffee powder Added
Hot coffee ready.....


See also:

  • Template Design pattern or Template Method Design pattern - Introduction
  • Template Design pattern or Template Method Design pattern - Real Time Example [Coffee]
  • Template Design pattern or Template Method Design pattern - Real Time Example [Car]
  • Template Design pattern or Template Method Design pattern - Class Diagram
  • Template Design pattern or Template Method Design pattern - Implementation [Building a House]
  • Template Design pattern or Template Method Design pattern - KeyPoints
  • All Design Patterns Links
  • 1 comment:

    1. This post was really helpful. Also love the way you explain. Thank you.

      Report template design

      ReplyDelete

    Tutorials