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图

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

总结

  • 装饰模式封装了什么变化?
    封装了为已有功能动态地添加更多功能,添加方式的变化。每一个变化也只需关注自身实现功能。
  • 重构代码或功能开发时如何使用装饰模式?
    把类中的装饰功能从类中搬移去除,这样可以简化原有的类。把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。
相关推荐
Poetinthedusk4 小时前
设计模式-工厂模式
设计模式·工厂方法模式
Poetinthedusk4 小时前
设计模式-模板方法模式
windows·设计模式·c#·wpf·模板方法模式
世洋Blog17 小时前
装饰器模式实践:告别臃肿的继承链,优雅解耦初始化状态管理
unity·设计模式·c#·装饰器模式
syt_101318 小时前
设计模式之-工厂模式
javascript·单例模式·设计模式
syt_101320 小时前
设计模式之-装饰器模式
设计模式·装饰器模式
看见繁华1 天前
C++ 设计模式&设计原则
java·c++·设计模式
雨中飘荡的记忆1 天前
观察者模式:从理论到生产实践
java·设计模式
阿波罗尼亚1 天前
Head First设计模式(十二) 设计原则 复合模式
设计模式
老朱佩琪!1 天前
Unity原型模式
开发语言·经验分享·unity·设计模式·原型模式