• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Template design pattern design pattern

Template Design Pattern

June 21, 2017 By j2eereference Leave a Comment

Template Design Pattern : Template design pattern comes under behavioral design pattern which provides a way to let objects communicate with each other.

In template design pattern , we have template method which defines the steps to execute an algorithm and subclasses will provide the implementation of these steps.

How to implement Template Pattern :

To implement template pattern we need to define a template method in an abstract class .Inside this method we define steps of algorithm  which are  to be implemented by subclasses .So in this way, template pattern provides a skeleton/framework to the subclasses.

Meal class: (Abstract Class)

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public abstract class Meal
{
  public final void prepareMeal()
  {
    getIngredients();
    cookingMeal();
    servingMeal();
  }
  public abstract void getIngredients();
  public abstract void cookingMeal();
  public void servingMeal() {
  System.out.println("Serving ordered meals to customers");
}
}

VegMeal : (Subclass Implementing abstract methods)

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class VegMeal extends Meal
{
  @Override
  public void getIngredients()
  {
    System.out.println("Getting Rice, Vegetable, flour and spices for veg meal preparation");
  }
  @Override
  public void cookingMeal()
  {
    System.out.println("Cooking veg meal");
  }
}

NonVegMeal : (Subclass Implementing abstract methods)

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class NonVegMeal extends Meal
{
   @Override
   public void getIngredients()
   {
     System.out.println("Getting Rice, chicken , flour and spices for non veg meal preparation");
   }
    
   @Override
   public void cookingMeal()
   {
     System.out.println("Cooking Non-Veg meal");
   }
}

TemplateDemo : (Client Class)

Java
1
2
3
4
5
6
7
8
9
10
11
public class TemplateDemo
{
    public static void main(String[] args)
    {
      Meal vegmeal = new VegMeal();
      vegmeal.prepareMeal();
 
      Meal nonvegmeal = new NonVegMeal();
      nonvegmeal.prepareMeal();
    }
}

OutPut:

Getting Rice, Vegetable, flour and spices for veg meal preparation
Cooking veg meal
Serving ordered meals to customers
Getting Rice, chicken, flour and spices for non-veg meal preparation
Cooking Non-Veg meal
Serving ordered meals to customers

In above example of the template method pattern, Meal is an abstract class with a template method defined as prepareMeal () which defines the steps (algorithm)involved in meal preparation. We declare template method as final so that it can’t be overridden by subclasses. The steps(algorithm)defined by prepareMeal () consists of three steps:getIngredients(),cookingMeal() and servingMeal().Default implementation of servingMeal () method is already given in the abstract Meal class ,so subclasses can override this implementation as it is. The getIngredients() and cookingMeal() methods are declared abstract so that subclasses need to implement them according to their need.

When to use Template Pattern:Use template pattern where you have business logic that   number of application components shared the same processes but the implementation was slightly different.

Some Use Cases:

1)In Java,Servlet class HttpServlet is an abstract class and its doGet, doPost, doPut methods are example of template method pattern usage in Java API.

2) AbstractSet and AbstractMap are example of template design pattern

3)In RealTime scenario,to construct a house architect prepares plan which consists of steps required to build house.

 

Related Posts

  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • GOF Design Patterns
  • History and Need for Design Patterns
  • Command Design Pattern
  • Proxy design pattern
  • Singleton in Clustered Environment
  • Observer pattern

Filed Under: Design Patterns Tagged With: behavioral design pattern, Template design pattern design pattern

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.