design pattern:: template method
TRANSCRIPT
![Page 1: Design Pattern:: Template Method](https://reader035.vdocuments.mx/reader035/viewer/2022080418/58a5c4981a28ab0b068b6e5d/html5/thumbnails/1.jpg)
Design Pattern::Template Method
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
![Page 2: Design Pattern:: Template Method](https://reader035.vdocuments.mx/reader035/viewer/2022080418/58a5c4981a28ab0b068b6e5d/html5/thumbnails/2.jpg)
Hello ! I am Neha Suwal
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Associate Software Engineer
Jyaasa Technologies
![Page 3: Design Pattern:: Template Method](https://reader035.vdocuments.mx/reader035/viewer/2022080418/58a5c4981a28ab0b068b6e5d/html5/thumbnails/3.jpg)
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
![Page 4: Design Pattern:: Template Method](https://reader035.vdocuments.mx/reader035/viewer/2022080418/58a5c4981a28ab0b068b6e5d/html5/thumbnails/4.jpg)
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?
![Page 5: Design Pattern:: Template Method](https://reader035.vdocuments.mx/reader035/viewer/2022080418/58a5c4981a28ab0b068b6e5d/html5/thumbnails/5.jpg)
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Structure
![Page 6: Design Pattern:: Template Method](https://reader035.vdocuments.mx/reader035/viewer/2022080418/58a5c4981a28ab0b068b6e5d/html5/thumbnails/6.jpg)
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
![Page 7: Design Pattern:: Template Method](https://reader035.vdocuments.mx/reader035/viewer/2022080418/58a5c4981a28ab0b068b6e5d/html5/thumbnails/7.jpg)
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.
![Page 8: Design Pattern:: Template Method](https://reader035.vdocuments.mx/reader035/viewer/2022080418/58a5c4981a28ab0b068b6e5d/html5/thumbnails/8.jpg)
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
ExampleOUTPUT:
MOO MOOOINK OINKQUACK QUACK
![Page 9: Design Pattern:: Template Method](https://reader035.vdocuments.mx/reader035/viewer/2022080418/58a5c4981a28ab0b068b6e5d/html5/thumbnails/9.jpg)
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
ExampleOUTPUT:
MOO MOO
uninitialized constant Animal::NotImplementedException (NameError)
![Page 10: Design Pattern:: Template Method](https://reader035.vdocuments.mx/reader035/viewer/2022080418/58a5c4981a28ab0b068b6e5d/html5/thumbnails/10.jpg)
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
![Page 11: Design Pattern:: Template Method](https://reader035.vdocuments.mx/reader035/viewer/2022080418/58a5c4981a28ab0b068b6e5d/html5/thumbnails/11.jpg)
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
● Understanding the sequence of flow
● Problem in maintenance
Disadvantage
![Page 12: Design Pattern:: Template Method](https://reader035.vdocuments.mx/reader035/viewer/2022080418/58a5c4981a28ab0b068b6e5d/html5/thumbnails/12.jpg)
Thank you!
Queries? Lets Discuss!!
https://np.linkedin.com/in/nsuwalhttps://github.com/ne-ha
http://jyaasa.comCopyright 2015. Jyaasa Technologies. Copyright 2016. Jyaasa Technologies. http://jyaasa.com