Design Pattern means that “we had some problem then we found the solution and gave name to this solution” and template method is also one of the solution so we need to find the problem that template pattern has solved and how ?
This design pattern comes under Behavioral Category (It tells that how objects are communicating with each other (objects have been created already)).
Template Method Pattern defines the skeleton of an algorithm in an operation, deferring some steps to sub classes. Template Method, lets sub classes redefine certain steps of an algorithm without changing the algorithm’s structure.
We want to change the behaviour of object, but don’t want to change the number of steps and order of steps in an algorithm.
we can refer some functions to derived class and can the behaviour of object and this solution called as “template design pattern“.
When there is duplicate code and we can make a single code by referring different behaviour in derived class.
g++ -std=c++11 templateMethod.cpp
Template Method Design Pattern
Non Virtual Base class Start() define skeleton of Algorithm, defere some step to derived class
Base Class function
Base Class Display Speed:10 Inclination:10
Base Class Start() End
Non Virtual Base class Start() define skeleton of Algorithm, defere some step to derived class
Derived Class function
Base Class Display Speed:20 Inclination:20
Base Class Start() End