装饰模式(大话设计模式)C/C++版本

装饰模式

需求分析:

复制代码
1. 选择服饰 => 服饰类
2. 输出结果
对象是人 => 人类

将Person类中一大堆服饰功能抽象出服饰类,然后通过Person类聚合服饰属性,通过Set行为来设置服饰属性,最后达到灵活打扮的效果

装饰模式

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

  1. 装饰对象的实现和如何使用该对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。

  2. 装饰模式是结构性的模式,将所需要的功能按正确的顺序串联起来进行控制

使用场景

复制代码
之前:旧类新增功能,向旧类添加新的属性和新的行为,增加了类的复杂度,破坏了开闭原则;
现在:装饰功能作为一个单独类存在,就可以在运行时灵活的增加到旧类中。

总结:

将类中的装饰功能从类中移除(简化类,避免类的膨胀),具体装饰类通过聚合父类来灵活增加父类功能;

有效的将一个大类的核心职责和装饰功能区分开了。

C++

cpp 复制代码
#include <iostream>
using namespace std;

// ConcreteComponent即Component
class Person
{
protected:
    string name;

public:
    Person(){};
    Person(string name) : name(name){};
    virtual void show()
    {
        cout << "装扮的" << name << endl;
    }
};

// Decorator类(装饰类),继承了Persson类,并且弱拥有Person类
class Finery : public Person
{
protected:
    Person *component;

public:
    Finery() : component(nullptr) {}
    void Decorate(Person *component)
    {
        this->component = component;
    }
    virtual void show()
    {
        if (component)
            component->show();
    }
};

// ConcreteDecorator类
class TShirts : public Finery
{
public:
    virtual ~TShirts() {}
    virtual void show()
    {
        cout << "Tshirt" << endl;
        Finery::show();
    }
};

// ConcreteDecorator类
class Jeans : public Finery
{
public:
    virtual ~Jeans() {}
    virtual void show()
    {
        cout << "Jeans" << endl;
        Finery::show();
    }
};

int main()
{
    Person *p = new Person("小菜");
    TShirts *oTShirt = new TShirts();
    Jeans *oJeans = new Jeans();
    oTShirt->Decorate(p);
    oJeans->Decorate(oTShirt);
    oJeans->show();

    delete p;
    p = nullptr;
    delete oTShirt;
    oTShirt = nullptr;
    delete oJeans;
    oJeans = nullptr;
    return 0;
}

C

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Component 类型定义
typedef struct Person
{
    char *name;
    void (*show)(struct Person *);
} Person;

// Decorator 类型定义
typedef struct Finery
{
    Person base;
    Person *component;
} Finery;

// ConcreteDecorator 类型定义
typedef struct TShirts
{
    Finery base;
} TShirts;

typedef struct Jeans
{
    Finery base;
} Jeans;

// 实现 Finery 的 show 方法
void finery_show(Finery *self)
{
    if (self->component && self->component->show)
    {
        self->component->show(self->component);
    }
}

// 实现 TShirts 的 show 方法
void tshirts_show(TShirts *self)
{
    printf("Tshirt\n");
    finery_show((Finery *)self);
}

// 实现 Jeans 的 show 方法
void jeans_show(Jeans *self)
{
    printf("Jeans\n");
    finery_show((Finery *)self);
}

// Finery 构造函数
Finery *finery_new(Person *component)
{
    Finery *self = malloc(sizeof(Finery));
    self->base.show = finery_show;
    self->component = component;
    return self;
}

// TShirts 构造函数
TShirts *tshirts_new(Finery *component)
{
    TShirts *self = malloc(sizeof(TShirts));
    self->base.base.show = tshirts_show;
    self->base.component = component;
    return self;
}

// Jeans 构造函数
Jeans *jeans_new(Finery *component)
{
    Jeans *self = malloc(sizeof(Jeans));
    self->base.base.show = jeans_show;
    self->base.component = component;
    return self;
}

// ConcreteComponent 方法实现
void person_show(Person *self)
{
    printf("装扮的 %s\n", self->name);
}

int main()
{
    Person *p = malloc(sizeof(Person));
    p->name = strdup("小菜");
    p->show = person_show;

    Finery *oTShirt = finery_new(p);
    TShirts *tshirts = tshirts_new(oTShirt);

    Finery *oJeans = finery_new(tshirts);
    Jeans *jeans = jeans_new(oJeans);

    jeans->base.base.show(jeans);

    free(p->name);
    free(p);
    free(tshirts);
    free(oTShirt);
    free(jeans);
    free(oJeans);
    return 0;
}
相关推荐
玖玥拾1 天前
C/C++ 基础笔记(十四)多态与模板编程
c语言·c++·多态·模板
Roann_seo%1 天前
C++文件操作完全指南:从文本读写到二进制文件处理
开发语言·c++
坚果派·白晓明1 天前
【鸿蒙PC】SDL3 适配:AtomCode + Skills 快速集成 NAPI 测试工具
c++·华为·ai编程·harmonyos·atomcode
凡人叶枫1 天前
Effective C++ 条款17:以独立语句将 newed 对象置入智能指针
java·linux·开发语言·c++·算法
我爱cope1 天前
【Agent智能体26 | 多智能体-多智能体工作流】
人工智能·设计模式·语言模型·职场和发展
凡人叶枫1 天前
Effective C++ 条款16:成对使用 new 和 delete 时要采取相同形式
开发语言·c++·effective c++
不吃土豆的马铃薯1 天前
C++ 高性能网络缓冲区 Buffer 源码解析
linux·服务器·开发语言·网络·c++
caimouse1 天前
Reactos 第1章 概述
c语言·开发语言·架构
.千余1 天前
【C++】C++继承入门(下):友元、静态成员与菱形继承的底层逻辑
开发语言·c++·笔记·学习·其他
初中就开始混世的大魔王1 天前
6 Fast DDS-传输层
开发语言·c++·中间件·信息与通信