代理模式

it2024-11-16  12

#include <iostream> using namespace std; /* 装饰模式是为已有功能动态添加更多功能的方法 装饰类和组件类的关系,装饰类为组件类添加功能,组件类 是装饰类的一个成员。这样可以在程序运行时动态的增删功 能,但是每一个装饰类不方便单独调试 使用装饰模式,可以达到串行化处理的效果 */ class Component { public: virtual void Show() = 0; }; class ConcreteComponent : public Component { public: void Show() { cout<<"ConcreteComponent Show"<<endl; } }; class Decorator : public Component { protected: Component* m_pComponent; public: void Show() { if (m_pComponent != NULL) { m_pComponent->Show(); } } }; class ConcreteDecoratorA : public Decorator { public: void SetComponent(Component* pComponent) { m_pComponent = pComponent; } void Show() { cout<<"ConcreteDecoratorA Show"<<endl; m_pComponent->Show(); } }; class ConcreteDecoratorB : public Decorator { public: void SetComponent(Component* pComponent) { m_pComponent = pComponent; } void Show() { cout<<"ConcreteDecoratorB Show"<<endl; m_pComponent->Show(); } }; int main() { ConcreteComponent c; ConcreteDecoratorA d1; ConcreteDecoratorB d2; d1.SetComponent(&c); d2.SetComponent(&d1); d2.Show(); return 0; }

转载于:https://www.cnblogs.com/yanjiu/archive/2012/08/05/2623700.html

相关资源:数据结构—成绩单生成器
最新回复(0)