听峰问雨 听峰问雨
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)

zfprotectors

默默学习er
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)
  • Python

  • Go

  • 数据结构与算法

  • 设计模式

    • 【设计模式】1 引言
    • 创建型

    • 结构型

    • 行为型

      • 【设计模式】行为型模式-Chain of Responsibility模式
      • 【设计模式】行为型模式-Command模式
      • 【设计模式】行为型模式-Interpreter模式
      • 【设计模式】行为型模式-Iterator模式
      • 【设计模式】行为型模式-Mediator模式
      • 【设计模式】行为型模式-Memento模式
      • 【设计模式】行为型模式-Observer模式
      • 【设计模式】行为型模式-State模式
      • 【设计模式】行为型模式-Strategy模式
      • 【设计模式】行为型模式-Template模式
      • 【设计模式】行为型模式-Visitor模式
  • 程序设计层
  • 设计模式
  • 行为型
zfprotectors
2022-05-18

【设计模式】行为型模式-Template模式

# 功能

定义一个操作中的算法的骨架,而将一些步骤延迟到子类中

# 解决

  • 主要解决:一些方法通用,却在每一个子类都重新写了这一方法
  • 何时使用:有一些通用的方法
  • 如何解决:将这些通用算法抽象出来
  • 关键代码:在抽象类实现,其他步骤在子类实现

# 优缺点

  • 优点:
    • 封装不变部分,扩展可变部分
    • 提取公共代码,便于维护
    • 行为由父类控制,子类实现
  • 缺点:每一个不同的实现都需要一个子类来实现,导致类的个数增加,使得系统更加庞大

# 应用场景

  • 应用实例:
    • 在造房子的时候,地基、走线、水管都一样,只有在建筑的后期才有加壁橱加栅栏等差异
    • spring 中对 Hibernate 的支持,将一些已经定好的方法封装起来,比如开启事务、获取 Session、关闭 Session 等
  • 使用场景:
    • 有多个子类共有的方法,且逻辑相同
    • 重要的、复杂的方法,可以考虑作为模板方法
  • 注意事项:为防止恶意操作,一般模板方法都加上 final 关键词

# 简单示例代码部分

Template.hpp

#ifndef _TEMPLATE_H_ 
#define _TEMPLATE_H_
#include <iostream>
using namespace std;
class AbstractClass{
public:
	virtual ~AbstractClass(){}
	void TemplateMethod(){
		this->PrimitiveOperation1();
		this->PrimitiveOperation2();
	}
protected:
	virtual void PrimitiveOperation1() = 0; 
	virtual void PrimitiveOperation2() = 0; 
	AbstractClass(){}
};

class ConcreteClass1:public AbstractClass {
public:
	ConcreteClass1(){}
	~ConcreteClass1(){} 
protected:
	void PrimitiveOperation1(){
		cout<<"ConcreteClass1...PrimitiveOperation1"<<endl;
	}
	void PrimitiveOperation2(){
		cout<<"ConcreteClass1...PrimitiveOperation2"<<endl;
	}
};

class ConcreteClass2:public AbstractClass {
public:
	ConcreteClass2(){}
	~ConcreteClass2(){}
protected:
	void PrimitiveOperation1(){
		cout<<"ConcreteClass1...PrimitiveOperation2"<<endl;
	}
	void PrimitiveOperation2(){
		cout<<"ConcreteClass2...PrimitiveOperation2"<<endl;
	}
};
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

main.cpp

#include "Template.h"
#include <iostream>
using namespace std;
int main() {
	AbstractClass* p1 = new ConcreteClass1(); 
	AbstractClass* p2 = new ConcreteClass2();
	p1->TemplateMethod(); 
	p2->TemplateMethod(); 
	return 0;
}
1
2
3
4
5
6
7
8
9
10
编辑 (opens new window)
#设计模式
上次更新: 2022/05/18, 15:47:37
【设计模式】行为型模式-Strategy模式
【设计模式】行为型模式-Visitor模式

← 【设计模式】行为型模式-Strategy模式 【设计模式】行为型模式-Visitor模式→

最近更新
01
LeetCode88 - 合并两个有序数组
06-22
02
LeetCode1 - 两数之和
06-22
03
LeetCode1603 - 设计停车系统
06-21
更多文章>
Theme by Vdoing | Copyright © 2021-2022 zfprotectors | 闽ICP备2021014222号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式