Template method design pattern for beginners.

Problem statement –

Please go through the strategy design pattern before you continue reading this article. From the strategy design pattern, we understand that; how to build an application, where you want the user to select the approach of solving/executing problem/algorithm at run time. For a moment, imagine template methods design pattern doing the same thing but in a controlled way.

Let us understand it through an example, there are 4 outstanding programmers. Like you! Writing code to solve a given problem. According to the strategy design patternit just wants a result. Not interested in how different programmers have written their code. And what approach/strategy they have followed to solve the problem. As long as they can implement various strategies in different strategy, concrete implementation.

Now those 4 programmers have joined the same software company and solves a given problem. This time as they have joined same company, all of them have to follow company-specific guidelines. i.e., how to write company standard code, what predefined third-party library they can use, or what they cannot use as per company’s build environment, and some other restrictions. Yes, they can use different algorithms to solve that problem, but at the same time, they have to follow all other restrictions imposed by the company. 

So, in summary, template method design pattern restricts concrete implementer to follow specific rules, and also gives some freedom to implement their own algorithms.

Strategy design pattern is an uncontrolled way of implementing things. In contrast, template method design pattern is a controlled way to achieve your software solution, so it is up to the designer in what way he would like to implement, both have their own merits and demerits. 

About template method design pattern –

In this design pattern, we will put all generic logic in base class and let the derived classes have their own specific implementations.

Structure –

template method DP
UML structure of template method design pattern.

Example –

In this example, you have a different source of playing the music. But what options your music system will show on the screen is restricted and driven by your base class.

template method Dp Ex
example of template method design pattern (Click on picture to see zoom view)




#include <iostream>
using namespace std;

class MusicSysem
{
    public:
        // go to next song
        void next()
        {
            cout<< "Play next song" << endl;
        }
        // go to previous song 
        void previous()
        {
            cout<< "Play previous song" << endl;
        }
        void pause()
        {
            cout<< "Pause song" << endl;
        }
        void off()
        {
            cout<< "Switch off the system" << endl;
        }
        void on()
        {
            cout<< "Switch on the system" << endl;
        }
        virtual void showDisplayMenue() = 0;
};
//*************************************************************************************
class Radio: public MusicSysem
{
    public:
        void showDisplayMenue()
        {
            cout<< "Hi welcome to radio system" << endl;
        }
};
//*************************************************************************************
class CDPlayer: public MusicSysem
{
    public:
        void showDisplayMenue()
        {
            cout<< "Hi welcome to CD Player system" << endl;
        }
};
//*************************************************************************************
class USB: public MusicSysem
{
    public:
        void showDisplayMenue()
        {
            cout<< "Hi welcome to USB Player system" << endl;
        }
};
/*
#######################################################################################
#########  __  __       _        ######################################################
######### |  \/  | __ _(_)_ __   ######################################################
######### | |\/| |/ _` | | '_ \  ######################################################
######### | |  | | (_| | | | | | ######################################################
######### |_|  |_|\__,_|_|_| |_| ######################################################
#######################################################################################
*/
int main(int argc, char** argv)
{
    MusicSysem *musicSysemPtr;
    int choice;
    cout<< "Welcome to my music system "<< endl;
    cout<< "Press 1 for Radio " << endl;
    cout<< "Press 2 for CDPlayer " << endl;
    cout<< "Press 3 for USB " << endl;
    cin >> choice;
    switch(choice)
    {
        case 1:
            musicSysemPtr = new Radio;
            musicSysemPtr->on();
            musicSysemPtr->showDisplayMenue();
            musicSysemPtr->next();
            musicSysemPtr->previous();
            musicSysemPtr->pause();
            musicSysemPtr->off();
            break;
        case 2:
            musicSysemPtr = new CDPlayer;
            musicSysemPtr->on();
            musicSysemPtr->showDisplayMenue();
            musicSysemPtr->next();
            musicSysemPtr->previous();
            musicSysemPtr->pause();
            musicSysemPtr->off();
            break;
        case 3:
            musicSysemPtr = new USB;
            musicSysemPtr->on();
            musicSysemPtr->showDisplayMenue();
            musicSysemPtr->next();
            musicSysemPtr->previous();
            musicSysemPtr->pause();
            musicSysemPtr->off();
            break;
        default:
            cout<< "You have entered wrong choice";
    }
    
    return 0;
}

///////////////////////////////////////////////////////////////////-------------OutPut
/*
$ ./a.exe
Welcome to my music system
Press 1 for Radio
Press 2 for CDPlayer
Press 3 for USB
1
Switch on the system
Hi welcome to radio system
Play next song
Play previous song
Pause song
Switch off the system
-------------------------------------
$ ./a.exe
Welcome to my music system
Press 1 for Radio
Press 2 for CDPlayer
Press 3 for USB
2
Switch on the system
Hi welcome to CD Player system
Play next song
Play previous song
Pause song
Switch off the system
-------------------------------------
$ ./a.exe
Welcome to my music system
Press 1 for Radio
Press 2 for CDPlayer
Press 3 for USB
3
Switch on the system
Hi welcome to USB Player system
Play next song
Play previous song
Pause song
Switch off the system

*/



Thanks for reading it. To learn more about design patterns and basic design principles, please see my web page.

Comments

Popular posts from this blog

Non-virtual interface idiom (NVI)

Architectural patterns => Mud to structure => layers.

Architectural style -> Adoptable system -> Reflection.