使用c语言实现一个装饰模式
时间: 2024-04-30 09:24:58 浏览: 19
装饰模式是一种结构性设计模式,它允许您通过将对象包装在具有新行为和责任的装饰器对象中来动态地修改对象的行为。使用C语言实现装饰模式时,您需要定义一个基本的接口或抽象基类,该接口或基类表示被装饰的对象。然后,您需要定义一个装饰器类,该类具有与基本接口或基类相同的接口,并具有一个指向基本对象的指针,以便可以对其进行装饰。
下面是一个使用C语言实现装饰模式的示例代码:
```
#include <stdio.h>
/* 基本接口或抽象基类 */
typedef struct Component {
void (*operation)(struct Component*);
} Component;
/* 具体对象 */
typedef struct ConcreteComponent {
Component base;
int state;
} ConcreteComponent;
void ConcreteComponent_operation(Component* component)
{
ConcreteComponent* self = (ConcreteComponent*) component;
printf("ConcreteComponent_operation: state=%d\n", self->state);
}
/* 装饰器类 */
typedef struct Decorator {
Component* component;
void (*operation)(Component*);
} Decorator;
void Decorator_operation(Component* component)
{
Decorator* self = (Decorator*) component;
self->component->operation(self->component);
}
/* 具体装饰器类 */
typedef struct ConcreteDecorator {
Decorator base;
int addedState;
} ConcreteDecorator;
void ConcreteDecorator_operation(Component* component)
{
ConcreteDecorator* self = (ConcreteDecorator*) component;
printf("ConcreteDecorator_operation: addedState=%d\n", self->addedState);
Decorator_operation(self->base.component);
}
int main()
{
/* 创建具体对象 */
ConcreteComponent concreteComponent;
concreteComponent.base.operation = ConcreteComponent_operation;
concreteComponent.state = 42;
/* 创建具体装饰器对象 */
ConcreteDecorator concreteDecorator;
concreteDecorator.base.component = (Component*) &concreteComponent;
concreteDecorator.base.operation = Decorator_operation;
concreteDecorator.addedState = 1337;
/* 调用被装饰对象的操作 */
concreteComponent.base.operation((Component*) &concreteComponent);
/* 调用装饰后的对象的操作 */
concreteDecorator.base.operation((Component*) &concreteDecorator);
return 0;
}
```
在示例代码中,我们定义了一个基本接口或抽象基类 `Component`,表示被装饰的对象,以及一个具体对象 `ConcreteComponent`,它实现了基本接口并具有一些状态。然后,我们定义了一个装饰器类 `Decorator`,它具有一个指向基本对象的指针,并实现了基本接口。最后,我们定义了一个具体装饰器类 `ConcreteDecorator`,它具有自己的状态并实现了基本接口,但是它还调用基本对象的操作。
在主函数中,我们首先创建一个具体对象 `concreteComponent`,然后创建一个具体装饰器对象 `concreteDecorator`,将其包装在 `concreteComponent` 上,并且调用了原对象和装饰后的对象的操作。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)