Template Method is a behavioral design pattern and it’s used to create a method stub and deferring some of the steps of implementation to the subclasses.Template method defines the steps to execute an algorithm and it can provide default implementation that might be common for all or some of the subclasses.
Let’s understand this pattern with an example, suppose we want to provide an algorithm to build a house. The steps need to be performed to build a house are – building foundation, building pillars, building walls and windows. The important point is that the we can’t change the order of execution because we can’t build windows before building the foundation. So in this case we can create a template method that will use different methods to build the house.
Now building the foundation for a house is same for all type of houses, whether its a wooden house or a glass house. So we can provide base implementation for this, if subclasses want to override this method, they can but mostly it’s common for all the types of houses.
To make sure that subclasses don’t override the template method, we should make it final.
Template Method Abstract Class
Since we want some of the methods to be implemented by subclasses, we have to make our base class as abstract class.
HouseTemplate.java
01 |
package com.journaldev.design.template; |
03 |
public abstract class HouseTemplate { |
05 |
//template method, final so subclasses can't override |
06 |
public final void buildHouse(){ |
11 |
System.out.println( "House is built." ); |
14 |
//default implementation |
15 |
private void buildWindows() { |
16 |
System.out.println( "Building Glass Windows" ); |
19 |
//methods to be implemented by subclasses |
20 |
public abstract void buildWalls(); |
21 |
public abstract void buildPillars(); |
23 |
private void buildFoundation() { |
24 |
System.out.println( "Building foundation with cement,iron rods and sand" ); |
buildHouse() is the template method and defines the order of execution for performing several steps.
Template Method Concrete Classes
We can have different type of houses, such as Wooden House and Glass House.
WoodenHouse.java
01 |
package com.journaldev.design.template; |
03 |
public class WoodenHouse extends HouseTemplate { |
06 |
public void buildWalls() { |
07 |
System.out.println( "Building Wooden Walls" ); |
11 |
public void buildPillars() { |
12 |
System.out.println( "Building Pillars with Wood coating" ); |
We could have overridden other methods also, but for simplicity I am not doing that.
GlassHouse.java
01 |
package com.journaldev.design.template; |
03 |
public class GlassHouse extends HouseTemplate { |
06 |
public void buildWalls() { |
07 |
System.out.println( "Building Glass Walls" ); |
11 |
public void buildPillars() { |
12 |
System.out.println( "Building Pillars with glass coating" ); |
Template Method Pattern Client
Let’s test our template method pattern example with a test program.
HousingClient.java
01 |
package com.journaldev.design.template; |
03 |
public class HousingClient { |
05 |
public static void main(String[] args) { |
07 |
HouseTemplate houseType = new WoodenHouse(); |
09 |
//using template method |
10 |
houseType.buildHouse(); |
11 |
System.out.println( "************" ); |
13 |
houseType = new GlassHouse(); |
15 |
houseType.buildHouse(); |
Notice that client is invoking the template method of base class and depending of implementation of different steps, it’s using some of the methods from base class and some of them from subclass.
Output of the above program is:
01 |
Building foundation with cement,iron rods and sand |
02 |
Building Pillars with Wood coating |
04 |
Building Glass Windows |
07 |
Building foundation with cement,iron rods and sand |
08 |
Building Pillars with glass coating |
10 |
Building Glass Windows |
Template Method Class Diagram
Template Method Pattern in JDK
- All non-abstract methods of java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.
- All non-abstract methods of java.util.AbstractList, java.util.AbstractSet and java.util.AbstractMap.
Important Points
- Template method should consists of certain steps whose order is fixed and for some of the methods, implementation differs from base class to subclass. Template method should be final.
- Most of the times, subclasses calls methods from super class but in template pattern, superclass template method calls methods from subclasses, this is known as Hollywood Principle – “don’t call us, we’ll call you.”.
- Methods in base class with default implementation are referred as Hooks and they are intended to be overridden by subclasses, if you want some of the methods to be not overridden, you can make them final, for example in our case we can make buildFoundation() method final because if we don’t want subclasses to override it.
Thats all for template method pattern implementation in java, I hope you liked it.
- 设计模式-模板方法设计模式--Template Method design pattern
/** * Abstract implementation of the {@link org.springframework.context.ApplicationContext} * interf ...
- 设计模式之 - 工厂方法模式 (Factory Method design pattern)
1. 模式意图: 定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类. 2. 别名(Virtual Constructor) 3. 结构 4. 工厂方法模式C ...
- Chain Of Responsibility Design Pattern Example
Avoid coupling the sender of a request to the receiver by giving more than one object a chance to ha ...
- Design Pattern - Service Locator Pattern--转载
原文地址:http://www.tutorialspoint.com/design_pattern/service_locator_pattern.htm The service locator de ...
- Design Pattern —— Prototype /Template Method/Iterator/Composite/Bridge
Prototype /Template Method/Iterator/Composite/Bridge 为什么把这五种设计模式放一起呢,没什么太高大上的原因,就是因为这五种模式JAVA开发最基本的特 ...
- 设计模式 - 模板方法模式(template method pattern) JFrame 具体解释
模板方法模式(template method pattern) JFrame 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考模板方法模式(templ ...
- java设计模式大全 Design pattern samples in Java(最经典最全的资料)
java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...
- java设计模式 模板方法模式Template Method
设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性.毫无疑问,设计模式于己 ...
- 深入浅出设计模式——模板方法模式(Template Method Pattern)
模式动机 模板方法模式是基于继承的代码复用基本技术,模板方法模式的结构和用法也是面向对象设计的核心之一.在模板方法模式中,可以将相同的代码放在父类中,而将不同的方法实现放在不同的子类中.在模板方法模式 ...
随机推荐
- VS工具相关内容
1.VS自带的Visual Studio中的<C# 语言规范>安装路径:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Sp ...
- C#计数器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- nohup 后台运行命令
在Linux上部署zipkin,在SSH客户端执行java -jar zipkin-server-1.21.0-exec.jar,启动成功,在关闭SSH客户端后,运行的程序也同时终止了,怎样才能保证在 ...
- nginx学习1
访问abc.xxx.com就跳转到www.xxx.com/abc.html server { listen 80; server_name abc.xxx.com; rewrite ^.*$ ww ...
- Spring boot with Apache Hive
5.29.1. Maven <dependency> <groupId>org.springframework.boot</groupId> <artif ...
- 几种查询方法(lambda Linq Enumerable静态类方式)
1.需要一个数据源类: using System; using System.Collections.Generic; namespace Linq { public class Student { ...
- WordPress部署
WordPress部署 WordPress是一个门户.博客网站的制作工具,php开发,自带后台,可以很简便的安装主题,还拥有一个庞大的主题网站生态. 软件下载:https://cn.wordpress ...
- win10 切换语言英文版
win10 切换英文语言 start > right click>control panel> time Language>laguage>Add >English ...
- Memo synEditor 当前行号
Memo 当前行号,坐标,位置 可以使用Memo的属性CaretPos.X来取行鼠标所在行的行数与鼠标所在行的第几位 Memo.CaretPos.X 光标或鼠标所在行的列号(第几位),从0开始计数Me ...
- 若(p,q)=1,则(p^n,q^n)=1
[若(p,q)=1,则(p^n,q^n)=1] 因(p,q)=1,则p,q可分别表示成如下的形式: p=A^a*B^b*C^c, q=D^d*E^e*F^f 显示ABC.DEF无交集.而p^n.q^n ...