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、调用:
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: };
5、结果
标签:std,cout,IComponent,component,模式,public,c++,Operate,装饰 From: https://www.cnblogs.com/xietianjiao/p/17514803.html