zoukankan      html  css  js  c++  java
  • (九)装饰模式

    1. 装饰模式

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

    Decorator

    Component 是定义一个对象接口,可以给这些对象动态地添加职责。ConcreteComponent 是一个具体的对象,也可以给这个对象添加一些职责。
    Decorator,装饰抽象类,继承了Component,从外类来扩展Component类的功能,但对于Component来说,无需知道Decorator的存在的。ConcreteDecorator就是具体的装饰对象,起到给Component添加职责的功能。

    可参考:
    装饰器模式(Decorator)——深入理解与实战应用

    2. 装饰模式示例

    #include <iostream>
    #include <string>
    #include <memory>
    using namespace std;
    
    class Component
    {
    public:
        Component() = default;
        virtual void Operation()=0;  // 如果是抽象基类,此处 =0 必须写,否则会编译出错。
    };
    
    class ConcreteComponent:public Component
    {
    public:
        ConcreteComponent() :Component(){}
    
        void Operation(){
            cout << "this is ConcreteComponent Operation()" << endl;
        }
    };
    
    class Decorator:public Component
    {
    public:
        void SetComponent(const shared_ptr<Component> &pComponent){
            m_pComponent = pComponent;
        }
        void Operation()override{
            m_pComponent->Operation();
        }
    private:
        shared_ptr<Component> m_pComponent;
    };
    
    
    class DecoratorA :public Decorator
    {
    public:
        void Operation()override{
            Decorator::Operation();
            AddedBehavior();
        }
    private:
        void AddedBehavior(){
            cout << "DecoratorA AddedBehavior()" << endl;
        }
    };
    
    
    class DecoratorB :public Decorator
    {
    public:
        void Operation()override{
            Decorator::Operation();
            AddedBehavior();
        }
    private:
        void AddedBehavior(){
            cout << "DecoratorB AddedBehavior()" << endl;
        }
    };
    
    int main()
    {
        shared_ptr<Component> pComponent(new ConcreteComponent());
        pComponent->Operation();
        cout << endl;
        
        shared_ptr<Decorator> pDec(new Decorator());
        pDec->SetComponent(pComponent);
        pDec->Operation();
        cout << endl;
    
        shared_ptr<DecoratorA> pDecA(new DecoratorA());
        pDecA->SetComponent(pDec);
        pDecA->Operation();
        cout << endl;
    
        shared_ptr<DecoratorB> pDecB(new DecoratorB());
        pDecB->SetComponent(pDecA);
        pDecB->Operation();
    }
    

    输出结果:

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

    3. 装饰模式的变体

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

    4. 装饰模式总结

    装饰模式是为已有功能动态地添加更多功能的一种方式。
    当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象。
    好处:
    把类中的装饰功能从类中搬移去除,这样可以简化原有的类。
    有效的把类的核心职责和装饰功能区分开了,而且可以去除相关类中重复的装饰逻辑。

  • 相关阅读:
    python基础(常量,数据类型,if)
    c字符串操作函数
    磁盘读写,合并排序字符串
    5个学生,3门成绩,输入信息,保存到文件
    关于MQTT、HTTP、WebService
    循环队列Circular Queue
    ISR中断服务程序
    PID控制
    Linux定时器接口
    Embedded Agent
  • 原文地址:https://www.cnblogs.com/walkinginthesun/p/9515726.html
Copyright © 2011-2022 走看看