• 6. 装饰器


    UML

    在这里插入图片描述

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

    测试代码

    #include 
    #include 
    #include //锁头文件
     
    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;
    }
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    输出

    ConcreteComponent
    具体装饰对象A的操作
    AddedBehavior
    具体装饰对象B的操作
    --endl--
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    装饰模式变身

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

    根据这段文字的描述,UML结构图变身后的形态
    在这里插入图片描述
    C++改写后的UML图和代码
    在这里插入图片描述

    代码:

    #include 
    #include 
    #include //锁头文件
     
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98

    输出

    装扮1
    运动鞋
    风衣
    装扮的:lkmao
     
    装扮2
    西服
    口罩
    墨镜
    装扮的:laoliu
    --endl--
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

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

  • 相关阅读:
    Spring Boot : Webflux 和 MVC 性能对比
    论文精读:带有源标签自适应的半监督域适应(Semi-Supervised Domain Adaptation with Source Label Adaptation)
    【面试题】为什么Object.prototype.toString.call() 可以准确判断对象类型?
    检索增强微调(RAFT)---使语言模型适应特定领域的 RAG
    github参与开源项目并上传修改
    java毕业生设计医疗健康管理平台会员管理子系统计算机源码+系统+mysql+调试部署+lw
    【JavaSE】抽象类和接口重点语法知识汇总(附有代码)
    机器学习(四十七):任何机器学习模型的可解释性(可视化篇)
    AppScan内部、外部手工扫描
    06-JVM 性能调优
  • 原文地址:https://blog.csdn.net/qq_40178082/article/details/132942175