Saturday 7 December 2013

Template Design pattern or Template Method Design pattern - Implementation [Building a House]


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

Click the below Image to Enlarge
Template Design pattern or Template Method Design pattern - Implementation [Building a House]

Template Design pattern or Template Method Design pattern - Implementation [Building a House] - Class Diagram
















HouseTemplate.java

public abstract class HouseTemplate
{

// Template method is final so subclasses can't override
public final void buildHouse()
{
buildFoundation();
buildPillars();
buildWalls();
buildWindows();
System.out.println("House is built.");
}

// Methods to be implemented by subclasses

public abstract void buildFoundation();

public abstract void buildPillars();

public abstract void buildWalls();

public abstract void buildWindows();

}

GlassHouse.java

public class GlassHouse extends HouseTemplate
{

@Override
public void buildFoundation()
{
System.out.println("Building foundation with cement,iron rods and sand");
}

@Override
public void buildPillars()
{
System.out.println("Building Pillars with glass coating");
}

@Override
public void buildWalls()
{
System.out.println("Building Glass Walls");
}

@Override
public void buildWindows()
{
System.out.println("Building Glass Windows");

}

}

WoodenHouse.java

public class WoodenHouse extends HouseTemplate
{

@Override
public void buildFoundation()
{
System.out.println("Building foundation with cement,iron rods,sand and wood");
}

@Override
public void buildPillars()
{
System.out.println("Building Pillars with Wood coating");
}

@Override
public void buildWalls()
{
System.out.println("Building Wooden Walls");
}

@Override
public void buildWindows()
{
System.out.println("Building Wooden Windows");

}

}

HousingClient.java

public class HousingClient
{

public static void main( String[] args )
{

System.out.println("Build a WoodenHouse\n");
HouseTemplate houseType = new WoodenHouse();

// using template method
houseType.buildHouse();
System.out.println("************************************************************");

System.out.println("Build a GlassHouse\n");
houseType = new GlassHouse();

houseType.buildHouse();
}

}

Output

Build a WoodenHouse

Building foundation with cement,iron rods,sand and wood
Building Pillars with Wood coating
Building Wooden Walls
Building Wooden Windows
House is built.
************************************************************
Build a GlassHouse

Building foundation with cement,iron rods and sand
Building Pillars with glass coating
Building Glass Walls
Building Glass Windows
House is built.


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 [Coffee]
  • Template Design pattern or Template Method Design pattern - KeyPoints
  • All Design Patterns Links
  • No comments:

    Post a Comment