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

2.Composite 模式结构图

3.实现

 #ifndef _COMPONENT_H_
#define _COMPONENT_H_ class Component
{
public:
Component();
virtual ~Component();
public:
virtual void Operation() = ;
virtual void Add(const Component& );
virtual void Remove(const Component& );
virtual Component* GetChild(int );
protected:
private:
}; #endif

Component.h

 #include "Component.h"

 Component::Component()
{ }
Component::~Component()
{ }
void Component::Add(const Component& com)
{ }
Component* Component::GetChild(int index)
{
return ;
}
void Component::Remove(const Component& com)
{ }

Component.cpp

 #ifndef _COMPOSITE_H_
#define _COMPOSITE_H_ #include "Component.h"
#include <vector>
using namespace std; class Composite:public Component
{
public:
Composite();
~Composite();
public:
void Operation();
void Add(Component* com);
void Remove(Component* com);
Component* GetChild(int index);
protected:
private:
vector<Component*> comVec;
}; #endif

Composite.h

 #include "Composite.h"
#include "Component.h"
#define NULL 0 //define NULL POINTOR Composite::Composite()
{ //vector<Component*>::iterator itend = comVec.begin();
}
Composite::~Composite()
{ }
void Composite::Operation()
{
vector<Component*>::iterator comIter = comVec.begin();
for (;comIter != comVec.end();comIter++)
{
(*comIter)->Operation();
}
}
void Composite::Add(Component* com)
{
comVec.push_back(com);
}
void Composite::Remove(Component* com)
{
comVec.erase(&com);
}
Component* Composite::GetChild(int index)
{
return comVec[index];
}

Composite.cpp

 #ifndef _LEAF_H_
#define _LEAF_H_ #include "Component.h" class Leaf:public Component
{
public:
Leaf();
~Leaf();
void Operation();
protected:
private:
}; #endif

Leaf.h

 #include "Leaf.h"
#include <iostream> using namespace std; Leaf::Leaf()
{ }
Leaf::~Leaf()
{ }
void Leaf::Operation()
{
cout<<"Leaf operation....."<<endl;
}

Leaf.cpp

 #include "Component.h"
#include "Composite.h"
#include "Leaf.h"
#include <iostream> using namespace std; int main(int argc,char* argv[])
{
Leaf* l = new Leaf();
l->Operation();
Composite* com = new Composite();
com->Add(l);
com->Operation();
Component* ll = com->GetChild();
ll->Operation(); return ;
}

main.cpp

Composite Pattern的更多相关文章

  1. 设计模式(十一):从文Finder中认识"组合模式"(Composite Pattern)

    上一篇博客中我们从从电影院中认识了"迭代器模式"(Iterator Pattern),今天我们就从文件系统中来认识一下“组合模式”(Composite Pattern).说到组合模 ...

  2. 浅谈设计模式--组合模式(Composite Pattern)

    组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...

  3. 深入浅出设计模式——组合模式(Composite Pattern)

    模式动机 对于树形结构,当容器对象(如文件夹)的某一个方法被调用时,将遍历整个树形结构,寻找也包含这个方法的成员对象(可以是容器对象,也可以是叶子对象,如子文件夹和文件)并调用执行.(递归调用)由于容 ...

  4. 二十四种设计模式:组合模式(Composite Pattern)

    组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...

  5. 乐在其中设计模式(C#) - 组合模式(Composite Pattern)

    原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...

  6. 第9章 组合模式(Composite Pattern)

    原文 第9章 组合模式(Composite Pattern) 概述: 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理 ...

  7. 【设计模式】组合模式 Composite Pattern

    树形结构是软件行业很常见的一种结构,几乎随处可见,  比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...

  8. C#设计模式之九组合模式(Composite Pattern)【结构型】

    一.引言 今天我们要讲[结构型]设计模式的第四个模式,该模式是[组合模式],英文名称是:Composite Pattern.当我们谈到这个模式的时候,有一个物件和这个模式很像,也符合这个模式要表达的意 ...

  9. NET设计模式 第二部分 结构性模式(10):组合模式(Composite Pattern)

    组合模式(Composite Pattern) ——.NET设计模式系列之十一 Terrylee,2006年3月 概述 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复 ...

  10. 组合模式(Composite Pattern) ------------结构型模式

    组合模式使用面向对象的思想来实现树形结构的处理和构件,描述了如何将容器对象和叶子对象进行递归组合,实现简单,灵活性好. 组合模式(Composite Pattern):组合多个对象形成树形结构以表示具 ...

随机推荐

  1. LeetCode OJ--Binary Tree Preorder Traversal

    http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 二叉树的先跟遍历,写的是递归版本,也可以使用stack来进行,替代了递归 ...

  2. Wireshark如何选择多行

    Wireshark如何选择多行   在Wireshark中,用户经常需要选择几行,然后进行批量操作,如导出或者分析.但Wireshark没有提供通过鼠标直接选择多行的功能.这个时候,用户需要采用标记分 ...

  3. mysql中TIMESTAMPDIFF简单记录

    1.  Syntax TIMESTAMPDIFF(unit,begin,end); 根据单位返回时间差,对于传入的begin和end不需要相同的数据结构,可以存在一个为Date一个DateTime 2 ...

  4. CodeForces - 393E Yet Another Number Sequence

    Discription Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  5. Spring Boot中使用AOP记录请求日志

    这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...

  6. mysql 数据库查询优化

    从上图可以看出,计算机系统硬件性能从高到代依次为: CPU——Cache(L1-L2-L3)——内存——SSD硬盘——网络——硬盘 由于SSD硬盘还处于快速发展阶段,所以本文的内容不涉及SSD相关应用 ...

  7. Linux图形界面和文字界面的切换

    Ctrl+Alt+F1~F6:切换到文字界面 登陆tty1~tty6终端, 运行等级为run level 3 Ctrl+Alt+F7 或者输入命令startX:切换到图形界面,运行等级为run lev ...

  8. [转载] C/C++中怎样获取日期和时间

    C/C++中怎样获取日期和时间摘要:  本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的数据结构和函数,并对计时.时间的获取.时间的计算和显示格式等方面进行了阐述.本文还通过大量的 ...

  9. GDB调试动态链接库

    http://cyukang.com/2012/06/25/gdb-with-libso.html http://cyukang.com/2011/05/06/valgrind.html

  10. Qnap 中VM下的win7

    因工作需要alone了Qnap 中VM下的win7,更改冲突项:1. 电脑名2. 必需卸载虚拟网卡,因为克隆系统的MAC地址相同.    添加网卡,指定IP Address.Subnet mask.G ...