[design pattern] Template Method

Posted by footbagger on Tue, 15 Feb 2022 13:10:25 +0100

Template Method

It is recommended to read the directory of C + + design patterns

motivation

  • In the process of software construction, for a task, it often has a stable overall operation structure, but each sub step has many change requirements, or it can not be realized at the same time with the overall structure of the task due to inherent reasons (such as the relationship between framework and application).
  • How to flexibly respond to the changes of each sub step or late implementation requirements on the premise of determining the stable operation structure?

Pattern definition

Define the skeleton of an algorithm in operation (stable), and delay (change) some steps to subclasses. Template Method enables subclasses to redefine (override) some specific steps of an algorithm without changing (reusing) the structure of the algorithm—— Design pattern GoF

SHOW ME THE CODE

Structured software design process

template1_app.cpp

#include "template1_lib.cpp"

class Application
{
  public:
	bool Step2()
	{
		cout << "myStep2" << endl;
		return true;
	}

	void Step4()
	{
		cout << "myStep4" << endl;
	}
};

int main()
{
	Library lib;
	Application app;
	//Overall process
	lib.Step1();

	if (app.Step2())
	{
		lib.Step3();
	}

	for (int i = 0; i < 4; i++)
	{
		app.Step4();
	}

	lib.Step5();
}

template1_lib.cpp

#include <iostream>

using namespace std;

//
class Library
{

public:
	void Step1()
	{
		cout << "Step1" << endl;
	}

	void Step3()
	{
		cout << "Step3" << endl;
	}

	void Step5()
	{
		cout << "Step5" << endl;
	}
};

Object oriented software design process

template2_app.cpp

#include "template2_lib.cpp"
#include <iostream>

using namespace std;

//Application Developer
class Application : public Library
{
  protected:
	virtual bool Step2()
	{
		//...  Subclass override implementation
		cout << "override Step2" << endl;
		return true;
	}

	virtual void Step4()
	{
		//...  Subclass override implementation
		cout << "override Step4" << endl;
	}
};

int main()
{
	Library *pLib = new Application();
	pLib->Run();

	delete pLib;
}

template2_lib.cpp

#include <iostream>

using namespace std;

//Library developer
class Library
{
  public:
    //Stable template method
    void Run()
    {
        Step1();

        if (Step2())
        { //Support polymorphic call of variant = = > virtual function
            Step3();
        }

        for (int i = 0; i < 4; i++)
        {
            Step4(); //Support polymorphic call of variant = = > virtual function
        }

        Step5();
    }
    virtual ~Library() {}

  protected:
    void Step1()
    { //stable
        cout << "Step1" << endl;
    }
    void Step3()
    { //stable
        cout << "Step3" << endl;
    }
    void Step5()
    {   //stable
        cout << "Step5" << endl;
    }

    virtual bool Step2() = 0; //change
    virtual void Step4() = 0; //change
};

contrast

Structured software design process: early binding, heavy burden on application developers

Object oriented software design process: bind late, write the main skeleton in the Library, and use virtual functions to support the change of subclasses

Template Method realizes late binding through virtual function (delaying method)

Structural design

Red is stable and blue changes

Summary of key points

  • Template Method pattern is a very basic design pattern, which is widely used in object-oriented system. It provides flexible extension points for many application frameworks with the simplest mechanism (polymorphism of virtual functions). It is the basic implementation structure of code reuse.
  • In addition to flexibly responding to the changes of sub steps, the reverse control structure of "don't call me, let me call you" is a typical application of Template Method.
  • In terms of specific implementation, the virtual methods called by the Template Method can have implementation or no implementation (abstract methods and pure virtual methods), but it is generally recommended to set them as protected methods.

Topics: C++ Design Pattern