设计模式之装饰模式Decorator
来源:互联网 发布:java log4j jar下载 编辑:程序博客网 时间:2024/06/02 03:17
动态地给一个对象扩展功能,而不是通过子类继承。
代码示例:
//The Window interface classclass Window{public://Draws the Windowvirtual void draw() = 0;//Returns a description of the Windowvirtual string getDescription() = 0;};//Implementation of a simple Window without any scrollbarsclass SimpleWindow : public Window{public:virtual void draw() override{//Draw window}virtual string getDescription() override{return "simple window";}};//abstract decorator class - note that it implements windowclass WindowDecorator : public Window{protected:Window* _windowToBeDecorated; //the window being decoratedpublic:WindowDecorator(Window* windowToBeDecorated){_windowToBeDecorated = windowToBeDecorated;}virtual void draw() override{_windowToBeDecorated->draw(); //Delegation}virtual string getDescription() override{return _windowToBeDecorated->getDescription(); //Delegation}};//The first concrete decorator which adds vertical scrollbar functionalityclass VerticalScrollBarDecorator : public WindowDecorator{public:VerticalScrollBarDecorator(Window* windowToBeDecorated): WindowDecorator(windowToBeDecorated){}virtual void draw() override{WindowDecorator::draw();drawVerticalScrollBar();}virtual string getDescription(){return WindowDecorator::getDescription() + ", including vertical scrollbars";}private:void drawVerticalScrollBar(){//Draw the vertical scrollbar}};//The second concrete decorator which adds horizontal scrollbar functionalityclass HorizontalScrollBarDecorator : public WindowDecorator{public:HorizontalScrollBarDecorator(Window* windowToBeDecorated): WindowDecorator(windowToBeDecorated){}virtual void draw() override{WindowDecorator::draw();drawHorizontalScrollBar();}virtual string getDescription(){return WindowDecorator::getDescription() + ", including horizontal scrollbars";}private:void drawHorizontalScrollBar(){//Draw the horizontal scrollbar}};void main(){//create a decorated window with horizontal and vertical scrollbars.Window* decoratedWindow = new HorizontalScrollBarDecorator(new VerticalScrollBarDecorator(new SimpleWindow));//Print the window's descriptiondecoratedWindow->getDescription();}
阅读全文
0 0
- 设计模式之decorator装饰模式
- 设计模式之-Decorator--装饰器模式
- 设计模式之Decorator(装饰者模式)
- 设计模式之(三、装饰模式Decorator)
- 设计模式之(九)装饰模式Decorator
- 设计模式之Decorator(装饰模式)
- 设计模式之装饰模式(Decorator Pattern)
- 【设计模式】之 Decorator 装饰模式
- 设计模式之八 --- 装饰模式(Decorator)
- 【设计模式】之装饰模式(Decorator)
- 设计模式之八 --- 装饰模式(Decorator)
- java设计模式之装饰模式Decorator
- 设计模式之装饰模式(Decorator)
- 设计模式之装饰模式(Decorator)
- 设计模式之装饰模式(Decorator)
- 设计模式之: Decorator(装饰器)模式
- C++设计模式之装饰(Decorator)模式
- 设计模式之装饰模式,decorator
- php递归获取一个数组中指定key的值
- Hibernate的检索方式
- 无法通过adb将文件push到system的问题
- C# 中的修饰符
- ArchSummit全球架构师峰会北京站2017
- 设计模式之装饰模式Decorator
- Vmware +RedHat5.5+Oracle 11G+Oracle Rac安装配置手册
- Java环境变量配置
- CAD转jpg如何设置输出质量
- 离不开的微信
- Hadoop支持的多种文件系统URI格式
- Android Studio打包错误Error:Error: Expected resource of type id [ResourceType]
- liferay Dynamic Query 查询
- Retrofit2.0+Rxjava2.0+MVP使用小记(一)