设计模式——组合模式(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设计模式——组合模式
前面的话 在程序设计中,有一些和“事物是由相似的子事物构成”类似的思想.组合模式就是用小的子对象来构建更大的对象,而这些小的子对象本身也许是由更小的“孙对象”构成的.本文将详细介绍组合模式 宏命令 宏 ...
随机推荐
- 使用commons-csv简单读写CSV文件
文章首发于我的github博客 需求 客户的开发测试环境将做迁移.因此需要对zookeeper上的重要的数据以CSV文件格式做备份. 本文通过Apache的commons-csv操作CSV文件.官网地 ...
- 《android开发艺术探索》读书笔记(十一)--Android的线程和线程池
接上篇<android开发艺术探索>读书笔记(十)--Android的消息机制 No1: 在Android中可以扮演线程角色的有很多,比如AsyncTask.IntentService.H ...
- JS判断输入类型是否为正整数
需要用到正则表达式:"/^+?[1-9][0-9]*$/". 例子如下: <!DOCTYPE html> <html> <head> <m ...
- POJ - 1190 生日蛋糕 dfs+剪枝
思路:说一下最重要的剪枝,如果当前已经使用了v的体积,为了让剩下的表面积最小,最好的办法就是让R尽量大,因为V = πR 2H,A' = 2πRH,A' = V / R * 2 ,最大的R一定是取当前 ...
- nyoj888 取石子(九) 反Nimm博弈
这题就是反Nimm博弈--分析见反Nimm博弈 AC代码 #include <cstdio> #include <cmath> #include <algorithm&g ...
- Spring cloud oauth2.0 access_token 永不失效设置方法
在AuthorizationServerConfigurerAdapter,重写一个TokenServices,注意这里的@Primary 非常重要,否则会有3个同类型的Bean,无法注入,会抛出以下 ...
- easywechat--在thinkPHP5中的使用
1. 安装 1.1 v-4.0 版本要求 PHP版本在7.0以上 1.2 在项目目录下运行以下命令 若未安装composer,则先安装composer -> http://docs.phpcom ...
- 普通权限拿webshell
普通权限拿webshell: 1.0day拿webshell:这个不多说.可以去网上搜索一些, 比如你找到你搞的网站cms是discz的,你可以搜索一些相 关0day直接拿 2.修改网站上传类 ...
- python函数式编程之迭代器
什么是迭代器 顾名思义,就是更新换代的意思 python中的迭代器就是根据上一个结果生成下一个结果,一直循环往复不断重复的过程 迭代器有两个特点: 1.不断重复同一个过程 2.根据上一个结果生成下一个 ...
- RTlinux3.2安装
( 1 ).前言 2003 年以后, fmslabs 的 RTLinux Free 版本为 3.2Pre ,和以前的 RTLinux 3.1 比较,不再需要必须从 2.4.4 的内核上安装. RTLi ...