Composite Pattern
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的更多相关文章
- 设计模式(十一):从文Finder中认识"组合模式"(Composite Pattern)
上一篇博客中我们从从电影院中认识了"迭代器模式"(Iterator Pattern),今天我们就从文件系统中来认识一下“组合模式”(Composite Pattern).说到组合模 ...
- 浅谈设计模式--组合模式(Composite Pattern)
组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...
- 深入浅出设计模式——组合模式(Composite Pattern)
模式动机 对于树形结构,当容器对象(如文件夹)的某一个方法被调用时,将遍历整个树形结构,寻找也包含这个方法的成员对象(可以是容器对象,也可以是叶子对象,如子文件夹和文件)并调用执行.(递归调用)由于容 ...
- 二十四种设计模式:组合模式(Composite Pattern)
组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...
- 乐在其中设计模式(C#) - 组合模式(Composite Pattern)
原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...
- 第9章 组合模式(Composite Pattern)
原文 第9章 组合模式(Composite Pattern) 概述: 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理 ...
- 【设计模式】组合模式 Composite Pattern
树形结构是软件行业很常见的一种结构,几乎随处可见, 比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...
- C#设计模式之九组合模式(Composite Pattern)【结构型】
一.引言 今天我们要讲[结构型]设计模式的第四个模式,该模式是[组合模式],英文名称是:Composite Pattern.当我们谈到这个模式的时候,有一个物件和这个模式很像,也符合这个模式要表达的意 ...
- NET设计模式 第二部分 结构性模式(10):组合模式(Composite Pattern)
组合模式(Composite Pattern) ——.NET设计模式系列之十一 Terrylee,2006年3月 概述 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复 ...
- 组合模式(Composite Pattern) ------------结构型模式
组合模式使用面向对象的思想来实现树形结构的处理和构件,描述了如何将容器对象和叶子对象进行递归组合,实现简单,灵活性好. 组合模式(Composite Pattern):组合多个对象形成树形结构以表示具 ...
随机推荐
- Python Challenge 第六关
第六关只有一张图和一个 PayPal 的链接,右键源代码注释中写着 PayPal 是作者要赞助的,跟题目没关系,其他的提示只有注释中写的个 zip.试过下图片,改图片扩展名等等都失败了,最后乱试改了下 ...
- elasticsearch入库错误:gc overhead导致数据节点脱离集群
https://my.oschina.net/u/3625378/blog/1793796
- Intellij IDEA创建项目
问题:无Java文件 解决方案: 打开file下面的project structure,在main目录下新建java文件,并将java文件设置为 Sources属性 问题:无resouces 解决方案 ...
- Codeforces Round #439 (Div. 2) 题解
题目链接 Round 439 div2 就做了两道题TAT 开场看C题就不会 然后想了好久才想到. 三种颜色挑出两种算方案数其实是独立的,于是就可以乘起来了. E题想了一会有了思路,然后YY出了一种 ...
- [原创][FPGA]有限状态机FSM学习笔记(一)
1. 概述--何为有限状态机FSM? 有限状态机-Finite State Machine,简写为FSM,是表示有限个状态及在这些状态之间的转移和动作等行为的数学模型,在计算机领域有着广泛的应用.通常 ...
- git commit或pull后恢复到原来版本
https://blog.csdn.net/litao31415/article/details/87713712
- Ubuntu 16.04下使用Wine安装Windows版的微信(不太完美)
说明: 真的不太完美,别试了:除了需要安装额外的输入法之后,无法上传图片和间接性的BUG出现等等问题. 建议安装网页版的微信:http://www.cnblogs.com/EasonJim/p/711 ...
- 【Maven jar】打包单个或多个文件,有依赖jar包的将架包一起打包成一个jar包供别的项目引用
之前有一片文章,是打包单个java文件的.这次想要将http://www.cnblogs.com/sxdcgaq8080/p/8398780.html 打包成jar包,发现这个java文件中引用了多 ...
- java资源分享、面试题资料、分布式大数据
马士兵大数据_架构师(1) 链接:http://pan.baidu.com/s/1qYTW1m0 密码:lxjd spring Cloud 链接:http://pan.baidu.com/s/1bzG ...
- jstl的函数
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@ tag ...