Template Design Pattern
If the framework
needs to own and control the algorithm but also call the subclasses as and when
needed, then use “Template Design Pattern”. Works on the Hollywood principle of
“Don’t call me, I will call you”.
1.
Create
a “Template Method” that defines a skeleton of an algorithm in an operation.
Template method defers some steps to the sub-classes. Additionally it lets
subclass redefine a certain step in processing using “Hooks”.
2.
Hooks
are methods that don’t do anything or have a default behavior. But subclass may
override that behavior.
3.
To
prevent subclasses from changing the algorithm, the template method is made
final.
It is also know as the algorithm method.
It is also know as the algorithm method.
4.
Factory
method is a specialization of Template Method.
5.
Subclasses
decide how to implement steps in an algorithm.
6.
All
duplicate codes are put into the superclass so that all subclasses can share
it.
An example can be
how Tea & Coffee are made.
Template Pattern
in java Arrays.class
Code link : https://github.com/sksumit1/DesignPatterns
No comments:
Post a Comment