Friday, January 26, 2018

Template method design pattern

Greetings!

Template Method pattern is a behavioral pattern.

When we have a common steps to do something but some steps vary, we can do it using bunch of if else statements. But the problem is things are tightly coupled and when new things need to be added we have to change our class. This leads our class difficult to main. For these kind of problems Template Method can be used.

We can have a super class to handle our common steps and let sub classes to define their own implements.

"Defines the skeleton of an algorithm in a method, deferring some steps to sub-classes. Template Method lets sub-classes redefine certain steps of an algorithm without changing the algorithm's structure."

                        
Image source wikipedia

A sample code

  • Don't call us, we'll call you. - allow low-level components to hook themselves into a system, but the high-level components determine when they are need and how.

No comments:

Post a Comment