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

装饰模式(大话设计模式)C/C++版本将 Person 类中一大堆服饰功能抽象出服饰类 然后通过 Person 类聚合服饰属性 通过 Set 行为来设置服饰属性 最后达到灵活打扮的效果装饰模式动态地给一个对象添加一些额外的职责 就增加功能来说装饰

大家好,欢迎来到IT知识分享网。

装饰模式

在这里插入图片描述

需求分析:

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

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

使用场景

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

C++

#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

#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; } 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/137013.html

(0)
上一篇 2025-06-23 17:45
下一篇 2025-06-23 18:00

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信