设计模式学习(十三):装饰模式
来源:互联网 发布:小海淘宝助手下载 编辑:程序博客网 时间:2024/05/19 22:03
在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
代码
#include <iostream>#include <string>using namespace std;class building{public: virtual void show(){}};class gymBuilding:public building{ //健身房public: gymBuilding(const char * n):name(n){} void show(){cout<<name<<endl;}private: string name;};class officeBuilding:public building{ //写字楼public: officeBuilding(const char * n):name(n){} void show(){cout<<name<<endl;}private: string name;};class decorator:public building{ //装饰类public: decorator(building *b):build(b){} virtual void myDecorator(){ build->show(); }private: building *build; };class MyDecorator:public decorator{public: MyDecorator(building *b):decorator(b){} void myDecorator(){ decorator::myDecorator(); addDecorator(); }private: void addDecorator(){ cout<<"MyDecorator"<<endl;}};
使用
int main(){ building *b = new gymBuilding("gymBuilding"); decorator *d = new MyDecorator(b); d->myDecorator(); delete b; delete d; return 1;}
结果
gymBuildingMyDecorator
0 0
- 设计模式学习(十三):装饰模式
- 设计模式之十三:装饰器模式
- 设计模式学习--装饰
- 设计模式学习----装饰模式
- 设计模式学习-----装饰模式
- 学习设计模式--装饰模式
- 设计模式学习--装饰模式
- 设计模式学习--装饰模式
- 学习设计模式-装饰模式
- 设计模式学习:装饰模式
- 设计模式C++学习笔记之十三(Decorator装饰模式)
- 设计模式C++学习笔记之十三(Decorator装饰模式)
- 设计模式C++学习笔记之十三(Decorator装饰模式)
- 设计模式C++学习笔记之十三(Decorator装饰模式)
- 设计模式C++学习笔记之十三(Decorator装饰模式)
- 设计模式C++学习笔记之十三(Decorator装饰模式)
- 设计模式C++学习笔记之十三(Decorator装饰模式)
- (十三)装饰模式
- Google C++ Style Guide 阅读笔记 4
- 博客搬家了
- 解决安装OpenGL时程序运行提示glut32.dll丢失的问题
- cocos2d-x 2.1.4 java.lang.UnsatisfiedLinkError
- 关于C语言里面的time
- 设计模式学习(十三):装饰模式
- Linux 多线程等待超时机制的实现:pthread_mutex_lock/pthread_cond_signal/pthread_mutex_unlock
- Java单例模式
- 第32课时,实践1,画出小星星
- 标签调用大全Dedecms5.6使用教程
- 设置Tomcat服务器自动加载web程序
- php使用session来保存用户登录信息
- 虚拟化环境高可靠存储的构架和配置
- 线程池