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):组合多个对象形成树形结构以表示具 ...
随机推荐
- 深入理解Atomic原子类
Atomic是基于unsafe类和自旋操作实现的,下面以AtomicInteger类为例进行讲解. 要理解Atomic得先了解CAS CAS CAS全程Compare And Swap ,是条并发原语 ...
- Android ANR原理分析
一.概述 ANR(Application Not responding),是指应用程序未响应,Android系统对于一些事件需要在一定的时间范围内完成,如果超过预定时间能未能得到有效响应或者响应时间过 ...
- ubuntu 卸载干净软件(包括配置文件)
var/cache/apt/archives occupying huge space I am in the process of cleaning up my system. And I see ...
- python3 查看已安装的模块
一.命令行下使用pydoc命令 在命令行下运行$ pydoc modules即可查看 二.在python交互解释器中使用help()查看 在交互式解释器中输入>>> help(&qu ...
- Flash如何为文字描边
可以使用墨水瓶工具,但是要先把文字打散(可以打散之后再组合起来)粗细和颜色都可以调,粗细就是笔触,颜色就是前景色(边框颜色)
- Solaris 目录与文件管理
熟悉系统目录结构 掌握27个常用命令 掌握针对目录.文件的操作 掌握查找与文件内容的操作 一.命令 命令:内部命令(不依赖其他文件,可以直接执行)与外部命令 .他是用于实现某一类功能的指令或程序,其执 ...
- python(19)- 列表生成式和生成器表达式练习Ⅰ
列表表达式 程序一: 常规写法: egg_list=[] for i in range(100): egg_list.append('egg%s' %i) print(egg_list) 列表表达式写 ...
- ffmpeg 视频教程 添加水印附源码
本文主要讲述如何利用Ffmpeg向视频文件 添加水印这一功能,文中最后会给出源代码下载地址以及视频 下载地址,视频除了讲述添加水印的基本原理以及代码实现,还提到了要注意的一些地方,因为直接运行 dem ...
- vs2012 MinGW编译ffmpeg 出现libavdevice/avdevice.c(38) : error C2059: 语法错误:“.”
利用vs2012编译ffmpeg出现以下错误: libavdevice/avdevice.c(38) : error C2059: 语法错误:“.” libavdevice/avdevice.c(40 ...
- 自己定义UITextField
目的是实现例如以下的效果: UITextField的leftView是自己定义的UIView,当中: 1.包括一个居中显示的icon.而且上,左,下各有1px的间隙 2.左上和左下是圆角,右边没有圆角 ...