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图

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

总结

  • 装饰模式封装了什么变化?
    封装了为已有功能动态地添加更多功能,添加方式的变化。每一个变化也只需关注自身实现功能。
  • 重构代码或功能开发时如何使用装饰模式?
    把类中的装饰功能从类中搬移去除,这样可以简化原有的类。把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。
相关推荐
刀法如飞11 小时前
领域驱动 vs 本体驱动:DDD 代码建模与 Ontology 语义建模的对比分析
设计模式·架构设计·领域驱动
我爱cope1 天前
【Agent智能体26 | 多智能体-多智能体工作流】
人工智能·设计模式·语言模型·职场和发展
咖啡八杯1 天前
【无标题】
java·后端·设计模式
折哥的程序人生 · 物流技术专研1 天前
Java 23 种设计模式:从踩坑到精通 | 适配器模式 —— 让不兼容的接口也能一起工作
java·设计模式·面试·适配器模式·单一职责原则
布朗克1681 天前
33 设计模式精讲
java·单例模式·设计模式
geovindu1 天前
python: Generators Pattern
开发语言·python·设计模式·生成器模式
雨浓YN1 天前
基于设计模式的Winform软件框架-01Xml\Log\Ini日志(单例模式+生产者消费者模式)
单例模式·设计模式
艾利克斯冰2 天前
Java 设计模式-行为型模式(更新中)
java·开发语言·设计模式
星心源七境2 天前
七境体系全解析:从六韬兵法到AI锁颜,一套贯穿古典智慧与现代应用的成长操作系统
人工智能·设计模式·设计