Design Pattern:: Template Method

Jyaasa 1,045 views 12 slides Nov 07, 2016
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

The template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in an operation, deferring some steps to subclasses.


Slide Content

Design Pattern::Template Method
http://jyaasa.comCopyright 2016. Jyaasa Technologies.

Hello !
I am Neha Suwal
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Associate Software Engineer
Jyaasa Technologies

http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Gang of Four Definition:

“Define a skeleton of an algorithm in an operation,
deferring some steps to subclasses. It lets subclasses
redefine certain steps of an algorithm without changing its
subclasses.”


Template Method

http://jyaasa.comCopyright 2016. Jyaasa Technologies.
That means, instead of having a huge if/switch/ case statement
somewhere to control what happens, Template Method lets object itself
determine what happens by using a method that all siblings inherit but
implement in their own way.
What does that mean?

http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Structure

http://jyaasa.comCopyright 2016. Jyaasa Technologies.
●`template_method` calls `step_one` method, `step_two` method and
`step_three` method
●`step_two` is a hook method. I.e It is declared in base class and
then defined in subclasses

http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Where to use?
●A class has more than one method that each contains an
if/switch/case statement with 3 or more possibilities.
●A class which has an if/switch/case statement that can have new
possibilities as conditions in the future.

http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Example
OUTPUT:

MOO MOO
OINK OINK
QUACK QUACK

http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Example
OUTPUT:

MOO MOO

uninitialized constant
Animal::NotImplemented
Exception (NameError)

http://jyaasa.comCopyright 2016. Jyaasa Technologies.
●No code duplication
●Since uses inheritance code is reused
●Lets subclasses decide how to implement steps in an algorithm.
Advantage

http://jyaasa.comCopyright 2016. Jyaasa Technologies.
●Understanding the sequence of flow
●Problem in maintenance
Disadvantage

Thank you!
Queries?
Lets Discuss!!
https://np.linkedin.com/in/nsuwal
https://github.com/ne-ha


http://jyaasa.comCopyright 2015. Jyaasa Technologies. Copyright 2016. Jyaasa Technologies. http://jyaasa.com