组合模式:将对象组合成树形结构以表示“部分-整体”的层次结构。
组合模式使得用户对单个对象和组合对象的使用具有一致性。
 
是一种结构型模式
 

 
使用场景:
1、用于对象的部分-整体层次结构,如树形菜单、文件夹菜单、部门组织架构图等;
2、对用户隐藏组合对象与单个对象的不同,使得用户统一地使用组合结构中的所有对象。
 #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++实现)的更多相关文章

  1. 16. 星际争霸之php设计模式--组合模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  2. Java设计模式——组合模式

    JAVA 设计模式 组合模式 用途 组合模式 (Component) 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有唯一性. 组合模式是一种结构型模 ...

  3. 【设计模式】Java设计模式 - 组合模式

    Java设计模式 - 组合模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 原创作品,更多关注我CSDN: 一个有梦有戏的人 准备将博客园.CSDN一起记录分享自己 ...

  4. c#设计模式-组合模式

    在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象和复合对象 ...

  5. [Head First设计模式]生活中学设计模式——组合模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

  6. JAVA 设计模式 组合模式

    用途 组合模式 (Component) 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有唯一性. 组合模式是一种结构型模式. 结构

  7. javascript设计模式-组合模式

    组合模式所要解决的问题: 可以使用简单的对象组合成复杂的对象,而这个复杂对象有可以组合成更大的对象.可以把简单这些对象定义成类,然后定义一些容器类来存储这些简单对象. 客户端代码必须区别对象简单对象和 ...

  8. 设计模式组合模式(Composite)精华

    23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例,他们帮助如何创建一个系统独立.这是一个这些对象和陈述的组合. 创建使用继承类的类架构更改实例.的对象类型模型的建 ...

  9. 设计模式 -- 组合模式 (Composite Pattern)

    定义: 对象组合成部分整体结构,单个对象和组合对象具有一致性. 看了下大概结构就是集团总公司和子公司那种层级结构. 角色介绍: Component :抽象根节点:其实相当去总公司,抽象子类共有的方法: ...

  10. javascript设计模式——组合模式

    前面的话 在程序设计中,有一些和“事物是由相似的子事物构成”类似的思想.组合模式就是用小的子对象来构建更大的对象,而这些小的子对象本身也许是由更小的“孙对象”构成的.本文将详细介绍组合模式 宏命令 宏 ...

随机推荐

  1. wamp server环境下mysql数据库的密码为什么修改不了?

    每次这个控制台,不输入密码可以直接用,用root登录都登录不了.修改root密码也修改不了.困惑? 经过不断的尝试终于找到解决的办法: 1,在mysql的配置文件my.ini的末尾添加 skip-gr ...

  2. 嵌入式linux------ffmpeg移植 解码H264(am335x解码H264到yuv420并通过SDL显示)

    /* 编译命令:arm-linux-gcc -o show2642 264showyuv2.c -I/usr/local/ffmpeg_arm/include/ -L/usr/local/ffmpeg ...

  3. freemarker写select组件报错总结(五)

    1.错误描述 六月 26, 2014 10:44:49 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template proc ...

  4. Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.commons.EmptyVisitor

    1.错误描述 2014-7-13 1:45:53 org.apache.struts2.spring.StrutsSpringObjectFactory info 信息: ... initialize ...

  5. org.apache.jasper.JasperException: /pages/path.jsp

    1.错误描述 三月 15, 2015 8:56:37 下午 org.apache.jasper.compiler.TldLocationsCache tldScanJar 信息: At least o ...

  6. ASP.NET Core 2.0 : 六. 举个例子来聊聊它的依赖注入

    本文通过一个维修工与工具库的例子形象的描述一下为什么要用依赖注入.它的工作原理是什么样的, 然后根据这个类比一下ASP.NET Core 中的依赖注入, 从而深刻了解它的使用方法.注意事项以及回收机制 ...

  7. SDK、JDK、JRE、ADB、AVD到底都是啥?

    SDK:Software Development Kit,软件开发工具包是一些被软件工程师用于为特定的软件包.软件框架.硬件平台.操作系统等创建应用软件的开发工具的集合,一般而言SDK即开发 Wind ...

  8. SpringBoot特性

    一.SpringBoot解决的问题 1.使编码变得简单 2.使配置变得简单 3.使部署变得简单 4.使监控变得简单 二.springboot主要特性 1.遵循习惯优于配置的原则.使用springboo ...

  9. 【Luogu1393】动态逆序对(CDQ分治)

    [Luogu1393]动态逆序对(CDQ分治) 题面 题目描述 对于给定的一段正整数序列,我们定义它的逆序对的个数为序列中ai>aj且i < j的有序对(i,j)的个数.你需要计算出一个序 ...

  10. POJ 2187 Beauty Contest(凸包,旋转卡壳)

    题面 Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the ...