博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Template Method Design Pattern in Java
阅读量:5891 次
发布时间:2019-06-19

本文共 5099 字,大约阅读时间需要 16 分钟。

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 .

HouseTemplate.java

01 package com.journaldev.design.template;
02  
03 public abstract class HouseTemplate {
04  
05     //template method, final so subclasses can't override
06     public final void buildHouse(){
07         buildFoundation();
08         buildPillars();
09         buildWalls();
10         buildWindows();
11         System.out.println("House is built.");
12     }
13  
14     //default implementation
15     private void buildWindows() {
16         System.out.println("Building Glass Windows");
17     }
18  
19     //methods to be implemented by subclasses
20     public abstract void buildWalls();
21     public abstract void buildPillars();
22  
23     private void buildFoundation() {
24         System.out.println("Building foundation with cement,iron rods and sand");
25     }
26 }

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;
02  
03 public class WoodenHouse extends HouseTemplate {
04  
05     @Override
06     public void buildWalls() {
07         System.out.println("Building Wooden Walls");
08     }
09  
10     @Override
11     public void buildPillars() {
12         System.out.println("Building Pillars with Wood coating");
13     }
14  
15 }

We could have overridden other methods also, but for simplicity I am not doing that.

GlassHouse.java

01 package com.journaldev.design.template;
02  
03 public class GlassHouse extends HouseTemplate {
04  
05     @Override
06     public void buildWalls() {
07         System.out.println("Building Glass Walls");
08     }
09  
10     @Override
11     public void buildPillars() {
12         System.out.println("Building Pillars with glass coating");
13     }
14  
15 }

Template Method Pattern Client

Let’s test our template method pattern example with a test program.

HousingClient.java

01 package com.journaldev.design.template;
02  
03 public class HousingClient {
04  
05     public static void main(String[] args) {
06  
07         HouseTemplate houseType = new WoodenHouse();
08  
09         //using template method
10         houseType.buildHouse();
11         System.out.println("************");
12  
13         houseType = new GlassHouse();
14  
15         houseType.buildHouse();
16     }
17  
18 }

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
03 Building Wooden Walls
04 Building Glass Windows
05 House is built.
06 ************
07 Building foundation with cement,iron rods and sand
08 Building Pillars with glass coating
09 Building Glass Walls
10 Building Glass Windows
11 House is built.

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  – “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.

 

Reference:   from our   Pankaj Kumar at the   blog.

转载地址:http://xufsx.baihongyu.com/

你可能感兴趣的文章
[CareerCup][Google Interview] 寻找动态的中位数
查看>>
javascript操作iframe的那些事
查看>>
servlet相关 jar包位置 BAE上部署web应用
查看>>
路徑 z
查看>>
cpu分析简介
查看>>
1.备忘录模式
查看>>
Html学习笔记3
查看>>
杭州见闻
查看>>
What is Xeround?
查看>>
[转载]jQuery上传插件Uploadify使用详解
查看>>
算法学习的轨迹(转)
查看>>
asmx-web-service-basic-authentication
查看>>
Excel转换成图片的操作方法
查看>>
MFC中读取和设置文件状态
查看>>
分页显示
查看>>
iOS中安全结束 子线程 的方法
查看>>
批处理学习笔记8 - 深入学习For命令1
查看>>
Object-c学习之路二(oc内存管理黄金法则1)
查看>>
python开发_python文件操作
查看>>
iPhone 已停用
查看>>