首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

C++设计方式Decorator简单实现

2013-09-12 
C++设计模式Decorator简单实现#includestdio.hclass VisualComponent {public:VisualComponent(){}virtu

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();}

热点排行