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图

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

总结

  • 装饰模式封装了什么变化?
    封装了为已有功能动态地添加更多功能,添加方式的变化。每一个变化也只需关注自身实现功能。
  • 重构代码或功能开发时如何使用装饰模式?
    把类中的装饰功能从类中搬移去除,这样可以简化原有的类。把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。
相关推荐
UXbot15 小时前
AI原型设计工具评测:从创意到交互式Demo,5款产品全面解析
前端·ui·设计模式·ai·ai编程·原型模式
橘子编程20 小时前
GoF 23 种设计模式完整知识总结与使用教程
java·c语言·开发语言·python·设计模式
UrSpecial20 小时前
设计模式:模板方法模式
设计模式·模板方法模式
如来神掌十八式1 天前
设计模式之装饰器模式
java·设计模式
qqxhb1 天前
26|Agent 设计模式:ReAct、Plan-and-Solve 与反射
设计模式·react模式·plan-and-solve·reflection模式
hssfscv2 天前
软件设计师下午题六——Java的各种设计模式
java·算法·设计模式
zhaoshuzhaoshu2 天前
设计模式之创建型设计模式详细解析(含示例)
单例模式·设计模式·架构
倚楼盼风雨2 天前
浅析设计模式-23种设计模式剖析
设计模式
Momentary_SixthSense3 天前
设计模式之工厂模式
java·开发语言·设计模式
Java码农也是农3 天前
Multi-Agent 系统设计模式
设计模式·agent·multi-agent