
目的
动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更为灵活。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
Class VisualComponent { Public: VisualComponent();
Virtual void Draw(); Virtual void Resize(); };
Class Decorator : public VisualComponent { Public: Decorator(VisualComponent*);
Virtual void Draw(); Virtual void Resize(); };
Void Decorator::Draw() { Return _component->Draw(); };
Void Decorator::Resize() { Return _component->Resize(); };
Class BorderDecorator : public Decorator { Public: BorderDecorator (VisualComponent*, int borderWidth); Virtual void Draw(); Private: void DrawBorder(int); Private: Int _width; };
Void BorderDecorator::Draw() { Decorator::Draw(); DrawBorder(_width); }
|
近期评论