用C++代码写一个模板方法模式
时间: 2023-12-27 08:46:34 浏览: 33
模板方法模式是一种行为设计模式,它定义了一个算法的骨架,将一些步骤延迟到子类中实现。该模式主要由一个抽象类和提供实现的具体子类组成。C语言可以通过函数指针实现模板方法模式,具体实现如下:
```c
#include <stdio.h>
/* 抽象基类 */
struct AbstractClass{
/* 模板方法,该方法定义了算法的骨架 */
void (*templateMethod)(struct AbstractClass*);
};
/* 具体实现类1 */
struct ConcreteClass1{
struct AbstractClass super; /* 具体实现类1继承了抽象基类 */
/* 子类实现的具体方法 */
void (*primitiveOperation1)(void){
printf("ConcreteClass1 implements primitiveOperation1\n");
}
/* 子类实现的具体方法 */
void (*primitiveOperation2)(void){
printf("ConcreteClass1 implements primitiveOperation2\n");
}
};
/* 具体实现类2 */
struct ConcreteClass2{
struct AbstractClass super; /* 具体实现类2继承了抽象基类 */
/* 子类实现的具体方法 */
void (*primitiveOperation1)(void){
printf("ConcreteClass2 implements primitiveOperation1\n");
}
/* 子类实现的具体方法 */
void (*primitiveOperation2)(void){
printf("ConcreteClass2 implements primitiveOperation2\n");
}
};
/* 具体实现类1实现的模板方法 */
void ConcreteClass1_templateMethod(struct AbstractClass* this){
struct ConcreteClass1* concrete = (struct ConcreteClass1*)this;
concrete->primitiveOperation1();
concrete->primitiveOperation2();
}
/* 具体实现类2实现的模板方法 */
void ConcreteClass2_templateMethod(struct AbstractClass* this){
struct ConcreteClass2* concrete = (struct ConcreteClass2*)this;
concrete->primitiveOperation2();
concrete->primitiveOperation1();
}
int main(int argc, char* argv[]){
/* 初始化具体实现类1对象 */
struct ConcreteClass1 concrete1 = {
.super = {.templateMethod = &ConcreteClass1_templateMethod},
.primitiveOperation1 = &ConcreteClass1_primitiveOperation1,
.primitiveOperation2 = &ConcreteClass1_primitiveOperation2
};
/* 初始化具体实现类2对象 */
struct ConcreteClass2 concrete2 = {
.super = {.templateMethod = &ConcreteClass2_templateMethod},
.primitiveOperation1 = &ConcreteClass2_primitiveOperation1,
.primitiveOperation2 = &ConcreteClass2_primitiveOperation2
};
/* 调用具体实现类1的模板方法 */
concrete1.super.templateMethod(&concrete1.super);
/* 调用具体实现类2的模板方法 */
concrete2.super.templateMethod(&concrete2.super);
return 0;
}
```
以上就是使用C语言实现模板方法模式的代码。每个具体实现类继承了抽象基类,并实现了自己的子类具体方法。模板方法以函数指针方式存在于抽象基类中,并被具体实现类所调用。通过模板方法模式,我们可以公用代码骨架,实现代码的复用性和扩展性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)