设计模式——组合模式(C++实现)
.png)
#include <iostream>
#include <string>
#include <vector> using namespace std; class STComponent
{
public:
STComponent()
{ }
STComponent(string strName): m_strName(strName)
{ }
virtual ~STComponent()
{ } /*
virtual void Add(STComponent* c);
virtual void Remove(STComponent* c) ;
virtual void Display(int iDepth);
*/ virtual void Add(STComponent* c) = ;
virtual void Remove(STComponent* c) = ;
virtual void Display(int iDepth) = ; string m_strName; }; class STLeaf: public STComponent
{
public:
STLeaf(string strName): STComponent(strName)
{ } virtual void Add(STComponent* c)
{
cout<< "Cann't Add to a leaf"<< endl;
}
virtual void Remove(STComponent* c)
{
cout<< "Cann't Remove from a leaf"<< endl;
}
virtual void Display(int iDepth)
{
cout<< string(iDepth, '-')<< m_strName<< endl;
}
}; class STComposite: public STComponent
{
public:
STComposite(string strName): STComponent(strName)
{ }
~STComposite()
{
m_vecStComposite.clear();
} virtual void Add(STComponent* c)
{
m_vecStComposite.push_back(c);
} virtual void Remove(STComponent* c)
{
for (typeof(m_vecStComposite.begin()) it = m_vecStComposite.begin(); it != m_vecStComposite.end();)
{
if (*it == c)
{
it = m_vecStComposite.erase(it);
cout<< "erase Succ: "<< (*it)->m_strName<< endl;
}
else
{
++it;
}
}
} virtual void Display(int iDepth)
{
cout<< string(iDepth, '-')<< m_strName<< endl;
for (size_t i = ; i < m_vecStComposite.size(); ++i)
{
m_vecStComposite[i]->Display(iDepth+);
}
} vector<STComponent*> m_vecStComposite;
}; int main(int argc, char* argv[])
{
//STLeaf* pstLeaf = new STLeaf("leafA");
//pstLeaf->Add(NULL);
//pstLeaf->Remove(NULL);
//pstLeaf->Display(10);
//delete pstLeaf; STComposite* root = new STComposite("root");
root->Add(new STLeaf("Leaf A"));
root->Add(new STLeaf("Leaf B")); STComposite* comp = new STComposite("Composite X");
comp->Add(new STLeaf("Leaf XA"));
comp->Add(new STLeaf("Leaf XB"));
root->Add(comp); STComposite* comp2 = new STComposite("Composite XY");
comp2->Add(new STLeaf("Leaf XYA"));
comp2->Add(new STLeaf("Leaf XYB"));
comp->Add(comp2); STLeaf* pstLeaf = new STLeaf("leaf D");
root->Add(pstLeaf);
root->Display(); // root->Remove(pstLeaf);
root->Remove(comp);
root->Display(); return ;
}
/////////////////////////////
[root@ ~/learn_code/design_pattern/16_composite]$ ./composite
-root
--Leaf A
--Leaf B
--Composite X
---Leaf XA
---Leaf XB
---Composite XY
----Leaf XYA
----Leaf XYB
--leaf D
erase Succ: leaf D
-root
--Leaf A
--Leaf B
--leaf D
设计模式——组合模式(C++实现)的更多相关文章
- 16. 星际争霸之php设计模式--组合模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- Java设计模式——组合模式
JAVA 设计模式 组合模式 用途 组合模式 (Component) 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有唯一性. 组合模式是一种结构型模 ...
- 【设计模式】Java设计模式 - 组合模式
Java设计模式 - 组合模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 原创作品,更多关注我CSDN: 一个有梦有戏的人 准备将博客园.CSDN一起记录分享自己 ...
- c#设计模式-组合模式
在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象和复合对象 ...
- [Head First设计模式]生活中学设计模式——组合模式
系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...
- JAVA 设计模式 组合模式
用途 组合模式 (Component) 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有唯一性. 组合模式是一种结构型模式. 结构
- javascript设计模式-组合模式
组合模式所要解决的问题: 可以使用简单的对象组合成复杂的对象,而这个复杂对象有可以组合成更大的对象.可以把简单这些对象定义成类,然后定义一些容器类来存储这些简单对象. 客户端代码必须区别对象简单对象和 ...
- 设计模式组合模式(Composite)精华
23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例,他们帮助如何创建一个系统独立.这是一个这些对象和陈述的组合. 创建使用继承类的类架构更改实例.的对象类型模型的建 ...
- 设计模式 -- 组合模式 (Composite Pattern)
定义: 对象组合成部分整体结构,单个对象和组合对象具有一致性. 看了下大概结构就是集团总公司和子公司那种层级结构. 角色介绍: Component :抽象根节点:其实相当去总公司,抽象子类共有的方法: ...
- javascript设计模式——组合模式
前面的话 在程序设计中,有一些和“事物是由相似的子事物构成”类似的思想.组合模式就是用小的子对象来构建更大的对象,而这些小的子对象本身也许是由更小的“孙对象”构成的.本文将详细介绍组合模式 宏命令 宏 ...
随机推荐
- CentOS命令修改系统时间同步
使用Centos,遇到本地时间对不上,直接敲命令:date -s "2016-01-08 15:15:15"是立即生效了,但是重启后,系统时间还是原来的. 修改了其一是没有办法奏 ...
- 使用MyBatis集成阿里巴巴druid连接池(不使用spring)
在工作中发现mybatis默认的连接池POOLED,运行时间长了会报莫名其妙的连接失败错误.因此采用阿里巴巴的Druid数据源(码云链接 ,中文文档链接). mybatis更多数据源参考博客链接 . ...
- JavaScript实现排序二叉树的相关算法
1.创建排序二叉树的构造函数 /** * 创建排序二叉树的构造函数 * @param valArr 排序二叉树中节点的值 * @constructor */ function BinaryTree(v ...
- 【前端】Vue2全家桶案例《看漫画》之七、webpack插件开发——自动替换服务器API-URL
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_app_7.html 项目github地址:https://github.com/shamoyuu/ ...
- 分析Android-Universal-Image-Loader的缓存处理机制
最近看了UIL中的缓存实现,才发现其实这个东西不难,没有太多的进程调度,没有各种内存读取控制机制.没有各种异常处理.反正UIL中不单代码写的简单,连处理都简单.但是这个类库这么好用,又有这么多人用,那 ...
- JAVA代码发送邮件示例和解释(二)
之前有使用过一次在程序中发邮件的功能,也写了一篇相关的笔记,当时使用的是163邮箱,经过测试多个163账号都是可行的.但是最近把账号换成中国移动@chinamobilesz.com这种结尾的时候,却一 ...
- DirectShow使用说明
1) 安装directX sdk 2)打开安装目录下的dshow.dsw,进行编译 3)在VC++的Tools/Option/Directory的Include和Library中分别加入 C:/DXS ...
- R语言︱缺失值处理之多重插补——mice包
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 笔者寄语:缺失值是数据清洗过程中非常重要的问题 ...
- Android线程间通信机制——深入理解 Looper、Handler、Message
在Android中,经常使用Handler来实现线程间通信,必然要理解Looper , Handler , Message和MessageQueue的使用和原理,下面说一下Looper , Handl ...
- Unity开发之存档和读档的三种实现方式
此文内容源自siki学院视频,仅供学习!视频链接地址:http://www.sikiedu.com/course/129 工程使用Unity 2017.3.0f3 (64-bit) 老司机读博客,了解 ...