设计模式——组合模式(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设计模式——组合模式
前面的话 在程序设计中,有一些和“事物是由相似的子事物构成”类似的思想.组合模式就是用小的子对象来构建更大的对象,而这些小的子对象本身也许是由更小的“孙对象”构成的.本文将详细介绍组合模式 宏命令 宏 ...
随机推荐
- Python 上下文管理器和else块
最终,上下文管理器可能几乎与子程序(subroutine)本身一样重要.目前,我们只了解了上下文管理器的皮毛--Basic 语言有with 语句,而且很多语言都有.但是,在各种语言中 with 语句的 ...
- 1 Python数据类型--
常见的Python数据类型: (1)数值类型:就是平时处理的数字(整数.浮点数) (2)序列类型:有一系列的对象并排或者排列的情况.如字符串(str),列表(list),元组(tuple)等 (3)集 ...
- qml 静态编译程序执行错误 无法定位程序输入点 CreateDXGIFactory2 于动态链接库 dxgi.dll 上
重新编译 qt 静态库即可,或 删除该动态库. -no-feature-d3d12 解决方案请参考如下网址: https://forum.qt.io/topic/78380/entry-point-n ...
- OpenStack中memcached的使用和实现
概述 主要分享下个人对Liberty版本openstack中cache使用的理解,由于作者水平有限,难免有所错误,疏漏,还望批评指正. openstack中可以使用cache层来缓存数据,Libert ...
- Centos小白学习
目录 查看机器设备信息 有线网络设置(必须) 设置主机名(必须) 设置sudo用户,一般默认只有root可以使用(必须) yum更新源(必须) 安装完Centos后桌面宽度不能自适应 Centos安装 ...
- R︱mlr包帮你挑选最适合数据的机器学习模型(分类、回归)+机器学习python和R互查手册
一.R语言的mlr packages install.packages("mlr")之后就可以看到R里面有哪些机器学习算法.在哪个包里面. a<-listLearners() ...
- mysql常用基础操作语法(二)~~对表的增删改操作【命令行模式】
1.修改表名:alert table oldtablename rename newtablename; 或者alert table oldtablename rename to newtablena ...
- 致IT同仁 — IT人士常犯的17个职场错误
记得刚参加工作时(那是97年),中国的IT刚刚兴起,那时,作为一个IT人士是一件很光荣的事,而那时的我正在做电气和电子相关的工作.99年第一次跳槽,进入了IT行业做软件开发.至今,中国的IT已经走过了 ...
- Codeforces Round #467 (Div. 1) B. Sleepy Game
我一开始把题目看错了 我以为是博弈.. 这题就是一个简单的判环+dfs(不简单,挺烦的一题) #include <algorithm> #include <cstdio> #i ...
- Vue2.0结合webuploader实现文件分片上传
Vue项目中遇到了大文件分片上传的问题,之前用过webuploader,索性就把Vue2.0与webuploader结合起来使用,封装了一个vue的上传组件,使用起来也比较舒爽. 上传就上传吧,为什么 ...