[C/C + + design pattern] Template Method

Posted by jwalsh on Fri, 22 Oct 2021 09:05:23 +0200

COF-23 pattern classification

For the purpose:

  • Creation mode: delay the creation of some objects to subclasses or other objects, so as to deal with the impact caused by the specific type implementation when the requirements change to object creation.
  • Structural pattern: obtain a more flexible structure through class inheritance or object combination, so as to deal with the impact of demand changes on the structure of objects.
  • Behavioral pattern: divide the responsibilities between classes and objects through class inheritance or object composition, so as to deal with the impact of demand changes on multiple interactive objects.

In terms of scope:

  • The class pattern deals with the static relationship between classes and subclasses. (inheritance scheme)
  • Object mode deals with the dynamic relationship between objects. (combined scheme)

Template method

Definition: defines the skeleton (stable) of an algorithm in operation, and delays (changes) 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. It is a behavioral model.

Usage scenario: in the process of software construction, for a task, it often has a stable overall operation structure, but each sub step has many requirements for change, or it cannot be implemented simultaneously with the overall structure of the task due to inherent reasons (such as the relationship between framework and application). How to flexibly deal with the changes of each sub step or late implementation requirements on the premise of determining the stable operation structure? This is what the template method pattern does.

Example: the following is an example of the software design process.

  • Before using the template method:


The whole process is actually fixed and consists of step 1~5. However, 1, 3 and 5 are completed by the Library, while 2 and 4 are completed by App developers. However, the main process must be written after all steps are developed.

At this time, a problem is involved. Although the main process is fixed, App personnel are only familiar with step 2 and step 4, and may not be familiar with the overall process. This will lead to errors in the main process, lead to software crash, and increase the development burden of App personnel in disguise.

Using the template method can avoid this problem and leave the main process to the familiar Library.

  • After using the template method:


App developers only need to be responsible for step2 and step4, and the main program is completed by a Library more familiar with the process, reducing the possibility of program errors.

Code implementation:

  • Before using the template method:
//Library developer
class Library {
public:
	void Step1() {
		//...
	}
	void Step3() {
		//...
	}
	void Step5() {
		//...
	}
};

//Application Developer
class Application {
public:
	bool Step2() {
		//...
	}
	void Step4() {
		//...
	}
};

int main()
{
	Library lib;
	Application app;
	lib.Step1();
	if (app.Step2()) {
		lib.Step3();
	}
	for (int i = 0; i < 4; i++) {
		app.Step4();
	}
	lib.Step5();
}
  • After using the template method:
//Library developer
class Library {
public:
    //Stable template method
    void Run() {
        Step1();
        if (Step2()) { //Support polymorphic calling of variant = = > virtual functions
            Step3();
        }
        for (int i = 0; i < 4; i++) {
            Step4(); //Support polymorphic calling of variant = = > virtual functions
        }
        Step5();
    }
    virtual ~Library() { }
protected:
    void Step1() { //stable
        //.....
    }
    void Step3() {//stable
        //.....
    }
    void Step5() { //stable
        //.....
    }
    virtual bool Step2() = 0;//change
    virtual void Step4() = 0; //change
};

//Application Developer
class Application : public Library {
protected:
    virtual bool Step2() {
        //... subclass override implementation
    }
    virtual void Step4() {
        //... subclass override implementation
    }
};

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

The structure diagram is as follows:

summary

  • The premise that the design pattern can be realized is that there is a stable point, that is, it has unchanged characteristics, such as the above operation process. If all the conditions are stable, the design pattern is useless.
  • Design pattern is to find isolation points between all stable and changing points. If all conditions are changing, any design pattern is useless.
  • Template Method pattern is a very basic design pattern, which is widely used in object-oriented systems. It provides flexible extension points for many application frameworks with the most concise mechanism (polymorphism of virtual functions). It is the basic implementation structure of code reuse.
  • 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 C++ Design Pattern