博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式 之 《组合模式》
阅读量:7244 次
发布时间:2019-06-29

本文共 1337 字,大约阅读时间需要 4 分钟。

 

GOOD:整体和部分可以被一致对待(如WORD中复制一个文字、一段文字、一篇文章都是一样的操作)

 

 

 

#ifndef __COMPOSITE_MODEL__#define __COMPOSITE_MODEL__ #include 
#include
#include
using namespace std;class Component{public: string m_strName; Component(string strName) { m_strName = strName; } virtual void add(Component* com) = 0; virtual void display(int nDepth) = 0;};class Leaf : public Component{public: Leaf(string strName) : Component(strName){} virtual void add(Component* com) { cout<<"leaf can't add"<
m_component;public: Composite(string strName) : Component(strName) {} virtual void add(Component* com) { m_component.push_back(com); } virtual void display(int nDepth) { string strtemp; for(int i=0; i
::iterator iter = m_component.begin(); while(iter!=m_component.end()) { (*iter)->display(nDepth+2); iter++; } }};#endif //__COMPOSITE_MODEL__/*#include "CompositeModel.h"int _tmain(int argc, _TCHAR* argv[]){Composite* p = new Composite("小王");p->add(new Leaf("小李"));p->add(new Leaf("小赵"));Composite* p1 = new Composite("小小五");p1->add(new Leaf("大三"));p->add(p1);p->display(1);return 0;}*/

 

 

 

转载于:https://www.cnblogs.com/MrGreen/p/3420529.html

你可能感兴趣的文章
YII assets使用
查看>>
未来已来——工作空间 WorkSpace 和物联网 IoT (2)
查看>>
从零开始玩人工智能-机器人服务-05
查看>>
Google API V2申请及Google Map简单应用例子
查看>>
CXF开发WebService客户端
查看>>
实现一个简单的等待进度盘
查看>>
安全、高效、专业 —— 码云企业版
查看>>
PHP全角转半角
查看>>
GetMessage()与PeekMessage(),以及WM_PAINT消息相关
查看>>
谷歌阻止宏基搭载不兼容的Android设备
查看>>
MySql双机热备解决方案
查看>>
接受数据的三种方式
查看>>
在线参考
查看>>
关于MyEclipse工程部署不能实时同步到Tomcat问题的解决
查看>>
本地MySQL数据库要访问远程MySQL数据库的表中的数据的实现
查看>>
一次完整的HTTP请求所经历的7个步骤
查看>>
线性代数--矩阵的逆
查看>>
android 内存 转载
查看>>
javascript中encodeURI和decodeURI方法
查看>>
ubuntu中的tomcat使用apr模式
查看>>