Template mode of design mode (C++)

Author: Zhai Tianbao Steven
Copyright statement: The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source

1. What is the template pattern?

       The template pattern is a behavioral software design pattern. A template algorithm is defined in the parent class, only the common part of the template is implemented, and the variable part is implemented in the subclass. Different subclasses have different effects on the same template. expansion and implementation.

       Advantages of template pattern:

  1. Good reusability. The public part in the parent class can be used multiple times and has good environmental adaptability.
  2. Good scalability. Subclasses extend the concrete implementation of parent class templates.
  3. Comply with the principle of opening and closing. Based on the template extension function, there is no need to change the original code.

      Disadvantages of template pattern:

  1. The number of classes increases. Every template-based implementation must define a subclass, which can easily bloat the amount of code.
  2. If the template of the parent class is changed, the subclasses must be changed synchronously.

2. Template mode

2.1 Structure diagram

       The client is the Main main function, the template framework is defined, and the implementation of the specific steps is handed over to the subclass.

2.2 Code example

       Scenario description: I am the owner of a computer shop, and I have trained a general computer installation tutorial for my apprentices. The apprentices refer to my tutorial to complete the installation according to the needs of different brands and different customers.

//Template.h
/****************************************************/
#pragma once
#include <iostream>
#include <unordered_map>
#include <vector>
#include <list>
#include <string>

using namespace std;

// 定义模板类-电脑
class Computer 
{
public:
	// 装配
	void install() {
		cout << "电脑安装开始。" << endl;
		cout << "电脑品牌为:" << getComputerType() << endl;
		installProcessor();
		installMemory();
		installCaliche();
		cout << "电脑安装结束。" << endl;
	}

private:
	// 获取电脑类型
	virtual string getComputerType() = 0;

	// 安装处理器
	virtual void installProcessor() = 0;

	// 安装内存
	virtual void installMemory() = 0;

	// 安装硬盘
	virtual void installCaliche() = 0;

};

// 实现具体模板类-惠普电脑
class HPComputer : public Computer 
{
private:
	// 获取电脑类型
	virtual string getComputerType() {
		return "惠普";
	}

	// 安装处理器
	virtual void installProcessor() {
		cout << "安装处理器:Inter 酷睿i7-1260P" << endl;
	}

	// 安装内存
	virtual void installMemory() {
		cout << "安装内存:32GB DDR4-3200MHz" << endl;
	}

	// 安装硬盘
	virtual void installCaliche() {
		cout << "安装硬盘:1TB SSD" << endl;
	}

};

// 实现具体模板类-戴尔电脑
class DellComputer : public Computer 
{
private:
	// 获取电脑类型
	virtual string getComputerType() {
		return "戴尔";
	}

	// 安装处理器
	virtual void installProcessor() {
		cout << "安装处理器:Inter 酷睿i7-1265U" << endl;
	}

	// 安装内存
	virtual void installMemory() {
		cout << "安装内存:16GB DDR4-3200MHz" << endl;
	}

	// 安装硬盘
	virtual void installCaliche() {
		cout << "安装硬盘:512GB SSD" << endl;
	}
};
//main.cpp
/****************************************************/
#include <iostream>
#include <string>
#include "Template.h"

using namespace std;

int main() 
{
	Computer *computerA = new HPComputer();
	Computer *computerB = new DellComputer();

	computerA->install();
	cout << endl;
	computerB->install();

	delete computerA;
	delete computerB;
	computerA = nullptr;
	computerB = nullptr;

	return 0;
}

       The program results are as follows.

       Friends who have read my "Builder Mode" article should find that the examples given in these two articles are all about installing computers. But in the builder mode, there is a "supervisor" who is responsible for implementing the specific steps and order of construction, while in the template mode, the parent class takes on this responsibility.

3. Summary

       I try my best to express my understanding of the template mode with more popular words and intuitive code routines. There may be some things that are not thoughtful. If you have different opinions, welcome to communicate in the comment area! Hope my example will help you to understand template pattern better.

       If the article helps you, you can give me a like to let me know, I will be very happy ~ come on!

Guess you like

Origin blog.csdn.net/zhaitianbao/article/details/130059180