Lesson 3 of design pattern -- Template Method

one   pattern classification

In terms of purpose:

Create pattern

Structural model

Behavioral model

In terms of scope:

The class pattern deals with the static relationship between classes and subclasses.

Object mode deals with the dynamic relationship between objects.

Classification of patterns from the perspective of packaging change:

Component collaboration:

Template Method

Strategy

Observer/Event

 

Single responsibility:

Decorator

Bridge

 

Object creation:

Factory Method

Abstact Factory

Prototype

Builder

 

Object performance:

Singleton

Flyweight

 

Interface isolation:

Facade

Proxy

Mediator

Adapter

 

Status change:

Memento

State

 

Data structure:

Composite

Iterator

Chain of

Resposibility

 

Behavior change:

Command

Visitor

 

Domain issues:

Interpreter

 

two   Refactoring to Patterns

Object oriented design pattern is "good object-oriented design". The so-called "good object-oriented design" refers to those designs that can meet the requirements of "coping with changes and improving reuse".

 

Modern software design is characterized by "frequent changes in requirements". The key point of design pattern is to "find the change point, and then apply the design pattern at the change point, so as to better respond to the change of demand". "When and where to apply design patterns" is more important than "understanding the structure of design patterns itself".

 

The application of design patterns should not be preconceived. First, using design patterns is the biggest misuse of design patterns. There is no one-step design pattern. Agile software development advocates "Refactoring to Patterns", which is recognized as the best design pattern method at present.

 

three   Key techniques of reconfiguration

Static -- > Dynamic

Early binding -- > late binding

Inheritance -- > combination

Compile time dependency -- > run time dependency

Tight coupling -- > loose coupling

 

four   Component collaboration mode

The first result after the professional division of modern software is the division of "framework and application". The "component cooperation" mode realizes the loose coupling between framework and application through late binding. It is a common mode for cooperation between the two.

 

Classic mode:

Template Method

Strategy

Observer/Event

 

five   Motivation of Template Method

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 deal with the changes of each sub step or late implementation requirements on the premise of determining the stable operation structure?

 

6. Code

template1_ Contents of lib. H file

 1 //Library developer
 2 
 3 //If in the actual project, the method should be in the corresponding cpp File implementation
 4 class Library
 5 {
 6 public:
 7 
 8     void Step1()
 9     {
10         //...
11     }
12 
13     void Step3()
14     {
15         //...
16     }
17 
18     void Step5()
19     {
20         //...
21     }
22 };

template1_app.h

 1 #pragma once
 2 
 3 //Application Developer
 4 
 5 //If in the actual project, the method should be in the corresponding cpp File implementation
 6 class Application
 7 {
 8 public:
 9     bool Step2()
10     {
11         //...
12     }
13 
14     void Step4()
15     {
16         //...
17     }
18 };

Main1.cpp

 1 //Application Developer
 2 
 3 #include "template1_lib.h"
 4 
 5 #include "template1_app.h"
 6 
 7 int main()
 8 {
 9     Library lib;
10     Application app;
11 
12     lib.Step1();
13     if (app.Step2())
14     {
15         lib.Step3();
16     }
17     
18     for (int i = 0; i < 4; i++)
19     {
20         app.Step4();
21     }
22     
23     lib.Step5();
24 }

7. Use the Template Method design pattern to modify the code

template2_lib.h

#pragma once

//Library developer

//If in the actual project, the method should be implemented in the corresponding cpp file
class Library
{
public:

	//Stable template method
	void Run()
	{
		Step1();
		if (Step2())                        //Support polymorphic calling of virtual functions
		{
			Step3();
		}

		for (int i = 0; i < 4; i++)
		{
			Step4();                       //Support polymorphic calling of virtual functions
		}

		Step5();
	}

	virtual ~Library()     //Virtual destructor
	{
		//...
	}
protected:

	void Step1()   //stable
	{
		//...
	}

	void Step3()   //stable
	{
		//...
	}

	void Step5()   //stable
	{
		//...
	}

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

 template2_app.h

#pragma once

#include "template2_lib.h"

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

	virtual void Step4()
	{
		//... subclass override implementation
	}

};

Main2.cpp

#include "template2_lib.h"

#include "template2_app.h"

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

	delete pLib;
}

  

eight   chart

 

 

 

 

 

nine   Template Method schema definition

Define the skeleton (stable) of the algorithm in an operation, 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. There are changes in stability. When the skeleton of the whole algorithm is unstable, it is no longer suitable for Template Method.

 

ten   structure

 

 

 

eleven   Summary of key points

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 (polymorphic call of virtual functions). It is the basic implementation structure of code reuse.

 

In addition to flexibly responding to sub step changes, the reverse control structure of "don't call me, let me call you" is a typical application scenario of Template Method.

 

In terms of specific implementation, the virtual function methods called by the Template Method can be implemented or not implemented (abstract methods and pure virtual methods). It is generally recommended to set them as protected methods.

 

 

The implementation mechanism of late binding of subclass virtual functions is realized by hanging a function pointer in the virtual function table.

 

Posted by PDXDesigner on Tue, 30 Nov 2021 19:55:56 -0800