C++设计模式Decorator简单实现
#include<stdio.h>class VisualComponent { public: VisualComponent(){} virtual void Draw() = 0; virtual void Resize() = 0;};class TextView : public VisualComponent{ public: TextView(){} virtual void Draw(); virtual void Resize();};void TextView::Draw(){ printf("Drawing TextView\n");}void TextView::Resize(){}class Decorator : public VisualComponent { public: Decorator(VisualComponent* component):_component(component){} virtual void Draw(); virtual void Resize(); private: VisualComponent* _component;};void Decorator::Draw() { _component->Draw();}void Decorator::Resize() { _component->Resize();}class BorderDecorator : public Decorator { public: BorderDecorator(VisualComponent* component, int borderWidth):Decorator(component), _width(borderWidth){} virtual void Draw(); private: void DrawBorder(int); private: int _width;};void BorderDecorator::DrawBorder(int width){ printf("Border:%d\n", width);}void BorderDecorator::Draw(){ Decorator::Draw(); DrawBorder(_width);}int main(){ TextView *textptr = new TextView; textptr->Draw(); BorderDecorator *bordertextptr = new BorderDecorator(textptr, 10); bordertextptr->Draw();}