1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Using Design Patterns in Java Projects

Template

Contents

keyboard_tab
Design Patterns
1
Design Patterns
PREVIEW2m 26s
2
Singleton Pattern
PREVIEW3m 13s
9
Decorator
7m 48s
10
Flyweight
9m 20s
11
Proxy
7m 36s
12
Facade
6m 5s
14
Command
11m 44s
15
Iterator
8m 24s
16
Visitor
9m 4s
17
Mediator
11m 38s
18
Memento
9m 48s
19
Observer
10m 40s
20
Strategy
8m 27s
21
State
10m 27s
22
Template
7m 56s
23
MVC
10m 27s
26
DAO
10m 27s
27

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
4h 56m
Students
20
Ratings
5/5
starstarstarstarstar
Description

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.

Prerequisites

Basic knowledge of Java programming.

 

Transcript

Hello, dear friends. In this video, we will examine the template design pattern. Let's begin. Have you faced any situation in your programming where you need to define a set of specific steps every time with a fixed order, so that this can be enforced by implementation classes?

If so, then the templateMethod pattern is the solution for you, which can be used to enforce these steps algorithm in an orderly manner. In object-oriented programming, the template method is one of the behavioral design patterns identified by GoF in the book, Design Patterns. The templateMethod is a method in a super class, usually an abstract super class that defines the skeleton of an operation in terms of a number of high-level steps. These steps are themselves implemented by additional helper methods in the same class as the templateMethod. In template pattern, an abstract class exposes defined ways or templates to execute its methods. Its sub classes can override the method implementation as per need, but the invocation is to be in the same way as defined by an abstract class. This pattern comes under behavior pattern category.

The template method pattern is a very common technique for reusing code. Template method design pattern is to define an algorithm as a skeleton of operations and leave the details to be implemented by the child classes. The overall structure and sequence of the algorithm are preserved by the parent class. Template means preset format like html templates, which has a fixed preset format. Similarly, in the template method pattern, we have a preset structure method called templateMethod, which consists of steps. These steps can be an abstract method that will be implemented by its sub classes. This design pattern is one of the easiest to understand and implement. This design pattern is used popularly in framework development. This helps to avoid code duplication also. The template method pattern's intent is to define the skeleton of a function in an operation, deferring some steps to its sub classes. The template method let's sub classes redefine certain steps of a function without changing the structure of the function. The HTTP servlet does this in the servlet API.

As you see in this UML, the strategy pattern has two participants. AbstractClass contains the templateMethod which should be made final, so that it cannot be overridden. This template method makes use of other operations available in order to run the algorithm, but is decoupled for the actual implementation of these methods. All operations used by this templateMethod are made abstract, so their implementation is deferred to sub classes. ConcreteClass implements all of the operations required by the templateMethod that were defined as abstract in the parent class. There can be many different concrete classes. Let's understand this pattern with an example. Suppose we want to provide an algorithm to build a house. The steps needed to be performed to build a house are: building foundation, building pillars, building walls, and windows. The important point is that 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. When we have predefined steps to achieve some algorithm or when we want to avoid duplicating code, we can use template design pattern. Now, I think with an example, this will be more understandable. Let's begin. First, create a new project and name it Template. In our example code, we will try and build houses with this pattern. Let's create an abstract class template, and name it HouseTemplate. We need a method to build a house. In this method, we will define our build order. We need to make the template method final so that sub classes don't override it. Let's define our operations, windows(). Now, foundation and windows will be in each house as standard, so we can define operations. Walls() ;. Pillars() ; And last one is Foundation(). Now, let's call them in build method. Done. Now, we will define our house types. WoodenHouse. As you remember, we haven't defined the operations of the pillar and wall methods. Let's override these methods for the wooden type. Let's continue with the GlassHouse.

If you want, we can add another house type also. The structure is ready. Let's test the structure. First, we need a template with chosen house type. GlassHouse() ;. Now, we will call the build method. Change house type. Call build again. Done. Let's run and look at the result. As you can see, our houses were built step by step. We have now finished the template design pattern. With this video, we have finished examining behavioral patterns. In the following video, we'll start looking at an architectural pattern known as MVC. See you in the following video.

 

About the Author
Students
413
Courses
35
Learning Paths
2

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics