Monday, 9 December 2013

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


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