6. 装饰器

UML

  • 聚合(Aggregation)关系:大雁和雁群,上图中空心菱形+箭头表示聚合关系
  • 组合(Composition)关系:大雁和翅膀 ,实心菱形+箭头表示组合(Composition)关系

测试代码

cpp 复制代码
#include <iostream>
#include <stdio.h>
#include <mutex>//锁头文件
 
using namespace std;
 
class Component{
public:
    virtual void Operation() = 0;
    virtual ~Component(){
 
    }
};
class ConcreteComponent:public Component
{
public:
    void Operation(){
        cout << "ConcreteComponent" << endl;
    }
};
 
class Decorator:public Component{
protected:
    Component *component = nullptr;
public:
    void SetComponent(Component *_component){
        this->component = _component;
    }
    void Operation(){
        if(component != nullptr){
            component->Operation();
        }
    }
};
class ConcreteDecoratorA:public Decorator
{
private:
    string addedState;
public:
    void Operation(){
        Decorator::Operation();
        addedState = "new State";
        cout << "具体装饰对象A的操作" << endl;
    }
};
 
class ConcreteDecoratorB:public Decorator
{
public:
    void Operation(){
        Decorator::Operation();
        AddedBehavior();
        cout << "具体装饰对象B的操作" << endl;
    }
private:
    void AddedBehavior()
    {
        cout << "AddedBehavior" << endl;
    }
};
int main(void)
{
    ConcreteComponent *c = new ConcreteComponent();
    ConcreteDecoratorA *d1 = new ConcreteDecoratorA();
    ConcreteDecoratorB *d2 = new ConcreteDecoratorB();
    d1->SetComponent(c);
    d2->SetComponent(d1);
    d2->Operation();
    cout << "--endl--" << endl;
    return 0;
}
 

输出

复制代码
ConcreteComponent
具体装饰对象A的操作
AddedBehavior
具体装饰对象B的操作
--endl--

装饰模式是利用SetComponent来对对象进行包装的。这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中

装饰模式变身

如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。同样道理,如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类

根据这段文字的描述,UML结构图变身后的形态

C++改写后的UML图和代码

代码:

cpp 复制代码
#include <iostream>
#include <stdio.h>
#include <mutex>//锁头文件
 
using namespace std;
 
//"Person"类(ConcreteComponent)
class Person{
public:
    Person(){
    }
    Person(string _name):name(_name){
    }
    virtual void Show(){
        cout << "装扮的:" << this->name << endl;
    }
private:
    string name;
};
//服饰类(Decorator)
class Finery:public Person
{
public:
 
    Person *component = nullptr;
    void Decorate(Person *_component){
        this->component = _component;
    }
    virtual void Show(){
        if(component != nullptr){
            component->Show();
        }
    }
};
class fengyi:public Finery
{
public:
    virtual void Show(){
        cout << "风衣" << endl;
        Finery::Show();
    }
};
 
class yundongxie:public Finery
{
public:
    virtual void Show(){
        cout << "运动鞋" << endl;
        Finery::Show();
    }
};
class mojing:public Finery
{
public:
    void Show(){
        cout << "墨镜" << endl;
        Finery::Show();
    }
};
class kouzhao:public Finery
{
public:
    void Show(){
        cout << "口罩" << endl;
        Finery::Show();
    }
};
class xifu:public Finery
{
public:
    void Show(){
        cout << "西服" << endl;
        Finery::Show();
    }
};
int main(void)
{
    cout << "装扮1" << endl;
    Person *p1 = new Person("lkmao");
    fengyi *fy = new fengyi();
    yundongxie *ydx = new yundongxie();
    fy->Decorate(p1);
    ydx->Decorate(fy);
    ydx->Show();
 
    cout << endl << "装扮2" << endl;
    Person *p2 = new Person("laoliu");
    mojing *mj = new mojing();
    kouzhao *kz = new kouzhao();
    xifu *xf = new xifu();
    mj->Decorate(p2);
    kz->Decorate(mj);
    xf->Decorate(kz);
    xf->Show();
 
    cout << "--endl--" << endl;
    return 0;
} 

输出

复制代码
装扮1
运动鞋
风衣
装扮的:lkmao
 
装扮2
西服
口罩
墨镜
装扮的:laoliu
--endl--

参考:https://blog.csdn.net/yueni_zhao/article/details/128946954

相关推荐
小贾要学习1 天前
【数据结构】C++实现红黑树
数据结构·c++
ajassi20001 天前
开源 C++ QT QML 开发(十七)进程--LocalSocket
c++·qt·开源
微露清风1 天前
系统性学习C++-第五讲-内存管理
java·c++·学习
星夜钢琴手1 天前
推荐的 Visual Studio 2026 Insider C++ 程序项目属性配置
c++·visual studio
kyle~1 天前
Qt---setAttribute设置控件或窗口的内部属性
服务器·前端·c++·qt
hsjkdhs1 天前
C++之多态
开发语言·jvm·c++
kyle~1 天前
C++STL---静态数组array
开发语言·c++
kk”1 天前
C++ List
开发语言·c++
HalvmånEver1 天前
红黑树实现与原理剖析(上篇):核心规则与插入平衡逻辑
数据结构·c++·学习·算法·红黑树
初圣魔门首席弟子1 天前
c++ bug 记录(merge函数调用时错误地传入了vector对象而非迭代器。)
java·c++·bug