c++实现装饰器模式

1、构建基本类的抽象

#include <iostream>

class IComponent
{
    public:
        virtual void Operate()=0;

    private:

};

2、构建基本类

class Component:public IComponent
{
    public:
        void Operate() override {
            std::cout << "基础操作..."<<std::endl;
        }

    private:

};

3、构建装饰器类

class Decorate:public IComponent
{

    private:
        IComponent * m_component;

    public:
        Decorate(IComponent * component){
            m_component = component;
        }

        void Operate() override{
            std::cout << "操作前处理..." << std::endl;
            m_component->Operate();
            std::cout << "操作后处理..." << std::endl;
        }
    private:

};

4、调用:

int main(){
Component* component = new Component();
Decorate* decorate = new Decorate(component);

decorate->Operate();

delete decorate;
delete component;

}

5、结果

 

posted @ 2023-06-29 17:35  卖雨伞的小男孩  阅读(78)  评论(0编辑  收藏  举报