首页 C++设计模式-复合委托继承

C++设计模式-复合委托继承

举报
开通vip

C++设计模式-复合委托继承  C++设计模式复合委托继承学习笔记  一、Composition(复合),表示has-a1、Adapter(适配器模式)表示Containerhas-aComponenttemplate>classqueue{...protected:Sequencec;//底层容器public://以下完全用c的操作函数完成boolempty()const{returnc.empty();}size_typesize()const{returnc.size();}referencefront(){returnc.front(...

C++设计模式-复合委托继承
  C++设计模式复合委托继承学习笔记  一、Composition(复合), 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 示has-a1、Adapter(适配器模式)表示Containerhas-aComponenttemplate>classqueue{...protected:Sequencec;//底层容器public://以下完全用c的操作函数完成boolempty()const{returnc.empty();}size_typesize()const{returnc.size();}referencefront(){returnc.front();}referenceback(){returnc.back();}//deque是两端可进可出,queue是末端进前端出(先进先出)voidpush(constvalue_type&x){c.push_back(x);}voidpop(){c.pop_front();}};//上述可以替换为以下形式templateclassqueue{...protected:dequec;//底层容器public://以下完全用c的操作函数完成boolempty()const{returnc.empty();}size_typesize()const{returnc.size();}referencefront(){returnc.front();}referenceback(){returnc.back();}//deque是两端可进可出,queue是末端进前端出(先进先出)voidpush(constvalue_type&x){c.push_back(x);}voidpop(){c.pop_front();}}; A(queue)拥有B(deque),但是A不做任何功能,A的功能全部都是由B提供的,这是一个特例。2、Composition(复合)关系下的构造和析构表示Containerhas-aComponent(1)构造由内而外Container的构造函数首先调用Component的default构造函数,然后才执行自己。Container::Container(...):Component(){...;}(2)析构由外而内Container的析构函数首先执行自己,然后才调用Component的析构函数。Container::~Container(){...~Component();}二、Delegation(委托)Compositionbyreference(复合by引用)表示String指针指向StringRep //Handle/Body(pImpl)//fileString.hppclassStringRep;classString{public:String();//构造函数String(constchar*s);//拷贝构造函数String(constString&s);//拷贝构造函数String&operator=(constString&s);//赋值运算符重载~String();//析构函数...private:StringRep*rep;//pimpl//指向处理的数据};//fileString.cpp#include"String.hpp"namespace{classStringRep{friendclassString;StringRep(constchar*s);~StringRep();intcount;char*rep;};}String::String(){}...三、Inheritance(继承),表示is-astruct_List_node_base{_List_node_base*_m_next;_List_node_base*_m_prev;};templatestruct_List_node:public_List_node_base//父类的数据被完全继承{_Tp_m_data;}; 继承最具价值的是与虚函数搭配起到的作用。1、Inheritance(继承)关系下的构造和析构baseclass的dtor必须是virtual,否则会出现undefinedbehavior(未定义行为)。(1)构造由内而外Devired的构造函数首先调用Base的default构造函数,然后才执行自己。Devired::Devired(...):Base(){...;}(2)析构由外而内Devired的析构函数首先执行自己,然后才调用Base的析构函数。Devired::~Devired(){...~Base();} 四、Inheritance(继承)withvirtualfunctioins(虚函数)子类继承父类,会继承父类的数据成员,即会开辟相应的空间,继承父类的函数时,仅是继承函数的使用权。non-virtual函数:你不希望derivedclass重新定义(override,复写)它。virtual函数:你希望derivedclass重新定义(override,复写)它,而且你对它已经有默认定义。purevirtual函数:你希望derivedclass一定要重新定义(override,复写)它,你对它没有默认定义。classShape{public:virtualvoiddraw()const=0;//纯虚函数virtualvoiderror(conststd::string&msg);//非纯虚函数(虚函数)intobjectID()const;//非虚函数...};classRetangle:publicShape{...}classEllipse:publicShape{...} 五、Inheritance(继承)withvirtual 设计模式之模板 方法 快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载 模式从main函数中开始执行,定义子类CMyDoc的对象myDoc,通过子类对象调用父类的方法,myDoc.OnFileOpen()。Serialize函数在父类中设计为虚函数,通过子类对象执行OnFileOpen,运行到Serialize函数时,会通过虚函数跳转到子类的Serialize函数,执行子类序列化缓存相关操作,子类Serialize函数执行完成之后,会回到父类的OnFileOpen函数,继续执行接下来的操作,OnFileOpen函数执行完成之后,会回来main函数中执行接下来的操作。继承,表示is-a,示例如下:#includeusingnamespacestd;classCDocument{public:voidOnFileOpen(){//这是一个算法,每一个cout代表一个实际动作cout<<"dialog..."<调用Derived的析构函数,调用Component的析构函数,最后调用基类的析构函数。图像示例实现2实验结果,先调用Component的构造函数,再调用基类的构造函数,最后调用子类的构造函数--->线调用子类的析构函数,调用父类的析构函数,最后调用Component的析构函数。七、Delegation(委托)+Inheritance(继承)---功能最强大这是可以将一种数据表现为多种不同的状态,例如将一份统计的数据,显示为柱状图,饼状图等等。classObserver;classSubject{intm_value;vectorm_views;public:voidattach(Observer*obs){m_views.push_back(obs);}voidset_val(intvalue){m_value=value;notify();}voidnotify(){for(inti=0;iupdate(this,m_value);}};classObserver{public:virtualvoidupdate(Subject*sub,intvalue)=0;}设计模式之Composite(复合)模式classComponent{intvalue;public:Component(intval){value=val;}//因为文件仅仅是最基础的,不存在子集,所以不能设计为纯虚函数,//而目录是有子目录,以及子文件,所以需要实现add函数,添加子目录virtualadd(Component*){}};classPrimitive:publicComponent{public:Primitive(intval):Component(val){}};classComposite:publicComponent{vectorc;public:Composite(intval):Component(val){}voidadd(Component*elem){c.push_back(elem);}};设计模式之Prototype(原型)模式假设我红线以上的内容是好几年前已经设计好的,但是我们又不能修改它,但是需要未来添加新的任务,即现在需要创建未来的class对象,所以你们后面发生的子类你们自己去创建自己,只要子类创建的对象可以被红线上层的框架看得到,所以可以拿到它当做一个蓝本,就可copy很多份。#includeenumImageType{LSAT,SPOT,};classImage{public:virtualvoiddraw()=0;staticImage*findAndClone(ImageType);protected:virtualImageTypereturnType()=0;//AseachsubclassofImageisdeclare,itregistersitsprototypestaticvoidaddPrototype(Image*image){_prototypes[_nextSlot++]=image;}private://addPrototype()saveseachregisterrdprototypestaticImage*_prototype[10];staticint_nextSlot;};Image*Image::_prototype[];intImage::_nextSlot;//ClientcallsthispublicstaticmemberfuntionwhenitisaninstanceofanImagesubclassImage*Image::findAddClone(ImageTypetype){for(inti=0;i<_nextSlot;i++){if(_prototype[i]->returnType()==type)return_prototype[i].clone();}}classLandSatImage:publicImage{public:ImageTypereturnType(){returnLSAT;}voiddraw(){cout<<"LandSatImage::draw()"<<_id<
本文档为【C++设计模式-复合委托继承】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
永兴文档
暂无简介~
格式:doc
大小:1MB
软件:Word
页数:35
分类:互联网
上传时间:2023-06-18
浏览量:1