Template Method Design Pattern

Template Method Design Pattern


  • Define the skeleton of an algorithm in an operation, deferring some steps to the subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithms structure.
  • In this pattern, the invariant parts of an algorithm are implemented once and the subclasses implement the behavior that varies.
  • The variant steps represent “hooks” or “placeholders”, that can or must be supplied by the subclass.
  • The designer mandates the required steps of an algorithm and the ordering of the steps, but allows the subclass to extend or replace some number of steps.


#include <iostream>

using namespace std;

// Base class

// Template class

class Account 

{    

public:  

void Start()

{        

cout << "Start ..." << endl; 

}       

void Allow() 

{          

cout << "Allow ..." << endl; 

}      

void End()

{       

cout << "End ..." << endl;   

}     


// Abstract Method  

virtual int MaxLimit() = 0; 


// Template Method   

void Withdraw(int amount) 

{       

Start();   

int limit = MaxLimit(); 

if ( amount < limit )

{         

Allow(); 

}          

else 

{   

cout << "Not allowed" << endl;  

}    

End();

}

};

// Derived class

class AccountNormal : public Account 

{    

public:      

 

int MaxLimit() 

{       

return 1000;    

}

};

// Derived class

class AccountPower : public Account 

{    

public:   

 

int MaxLimit()

{       

return 5000;  

}

};


int main() 

{    

AccountPower power; 

power.Withdraw(1500);  

AccountNormal normal; 

normal.Withdraw(1500);

}


Consequences:

In template methods a parent class calls the operations of a subclass and not the other way round. 

It is important for template methods to specify which operations are hooks(may be overriden) and which are abstract(must be overriden)


When to use this pattern:

Use the template method pattern when

The invariant parts of the algorithm are implemented once and the variant parts are left to the subclasses.

Common behaviour among subclasses should be factored out and localized in a common class to avoid code duplication.

You need to control subclass extensions.A template method can be defined that calls a “hook” operation at specific points. This hook operations can be overridden by the subclasses. This way the extension can be performed only at the hook points.


Difference between Strategy and Template Method


Strategy

Template Method

Uses composition to vary an algorithm

Uses inheritence to vary an algorithm.

The whole algorithm changes by changing the strategy

Only the part of the algorithm changes.

Used when the entire behaviour has different implementations.

Used when some steps of algorithm can have different implementations.

New strategies can be created by defining new classes.

The algorithm can be changed only at fixed set of points (hooks)

Replacement for if-else, switch case statements.

Code duplication is avoided by factoring out the common code in parent class.


Setup VSCode with Salesforce

Step by Step guide to setup VSCode with Salesforce Install vscode Install Salesforce CLI (developer.salesforce.com/tools/sfdxcli) Perform fo...