3.设计模式-装饰模式

定义:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

需求

要求写一个可以给人搭配不同的服饰的系统,比如类似QQ、网络游戏或论坛都有的Avatar系统。

需求分析

  • 针对一个对象会动态新增代码
  • 增加的顺序不固定

代码

被装饰类

c 复制代码
typedef struct Person {
    char *name;
    void (*show)(struct Person *);
} Person;
void PersonShow(Person *mthis) {
    printf("装扮的%s\n", mthis->name);
}
Person *ConstructPerson(char *name) {
    Person *obj = (Person *)malloc(sizeof(Person));
    obj->name = name;
    obj->show = PersonShow;
    return obj;
}

装饰类

c 复制代码
typedef struct Decorator {
    Person base;
    Person *decorated;
} Decorator;
void DecoratorShow(Person *mthis) {
    printf("%s ", mthis->name);
    ((Decorator *)mthis)->decorated->show(((Decorator *)mthis)->decorated);
    return;
}
Decorator *ConstructDecorator(char *decoratorName, Person *p) {
    Decorator *obj = (Decorator *)malloc(sizeof(Decorator));
    obj->base.name = decoratorName;
    obj->decorated = p;
    obj->base.show = DecoratorShow;
    return obj;
}

客户端使用

c 复制代码
int main() {
    Person *p = ConstructPerson("小菜");
    Decorator *pqx = ConstructDecorator("破球鞋", p);
    Decorator *kk = ConstructDecorator("垮裤", (Person *)pqx);
    Decorator *dtx = ConstructDecorator("大T恤", (Person *)kk);
    dtx->base.show((Person *)dtx);
    return 0;
}

当前代码是在person类前装饰,想改变装饰方式,在代码前后都做操作怎么弄?

新建一个类继承decorate, 重新实现一show方法和"构造函数"。------符合开闭原则

对C语言实现类之间关系的新认识

  • 如何继承
  • 如何聚合
c 复制代码
typedef struct Decorator {
    Person base;	// Decorator类继承Person
    Person *decorated; // Decorator类由Person类聚合
} Decorator;

UML图

"构造函数"和函数调用时的本对象没画

总结

  • 装饰模式封装了什么变化?
    封装了为已有功能动态地添加更多功能,添加方式的变化。每一个变化也只需关注自身实现功能。
  • 重构代码或功能开发时如何使用装饰模式?
    把类中的装饰功能从类中搬移去除,这样可以简化原有的类。把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。
相关推荐
会员果汁43 分钟前
14.设计模式-备忘录模式
设计模式·备忘录模式
xiaolyuh12311 小时前
Spring 框架 核心架构设计 深度详解
spring·设计模式·spring 设计模式
GISer_Jing21 小时前
智能体工具使用、规划模式
人工智能·设计模式·prompt·aigc
GISer_Jing1 天前
AI Agent:学习与适应、模型上下文协议
人工智能·学习·设计模式·aigc
小马爱打代码1 天前
MyBatis设计模式:构建者、工厂、代理模式
设计模式·mybatis·代理模式
月明长歌1 天前
Javasynchronized 原理拆解:锁升级链路 + JVM 优化 + CAS 与 ABA 问题(完整整合版)
java·开发语言·jvm·安全·设计模式
会员果汁1 天前
12.设计模式-状态模式
设计模式·状态模式
Yu_Lijing1 天前
基于C++的《Head First设计模式》笔记——抽象工厂模式
c++·笔记·设计模式
会员果汁1 天前
13.设计模式-适配器模式
设计模式·适配器模式
GISer_Jing2 天前
AI:多智能体协作与记忆管理
人工智能·设计模式·aigc