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):组合多个对象形成树形结构以表示具 ...
随机推荐
- 解决win2008下IIS7的HTTP500错误
造成500错误常见原因有:ASP语法出错.ACCESS数据库连接语句出错.文件引用与包含路径出错.使用了服务器不支持的组件如FSO等.另外,对于win2008的IIS默认不显示详细出错信息的问题以下就 ...
- Day 17 编码+文本编辑+函数
知识点篇: #! /usr/bin/env python # -*- coding: utf-8 -*- # __author__ = "DaChao" # Date: 2017/ ...
- 根据ipnut的maxlength实时提示输入的字符长度
$(function(){ $("body").on("focus","input,textarea", function() { if(! ...
- 拼题 L2-001 紧急救援 最短路计数+记录路径
https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 L2-001 紧急救援 (25 分) 作 ...
- JVM, JRE 和JDK
JVM -- java virtual machine A Java virtual machine (JVM) is a process virtual machine that can execu ...
- MX
A mail exchanger record (MX record) is a type of resource record in the Domain Name System that spec ...
- Java Web工程连接MySQL数据库及Tomcat服务器页面中文乱码
Java Web工程连接MySQL数据库 一. 准备工作 1.下载连接MySQL数据库的JDBC (可以去官网下,也可以去百度云找) 2.将下载的jar文件复制到Tomcat的lib目录下 3.新建一 ...
- 阿里云云服务器ubuntu配置nginx+uwsgi+django记录文档
1 安装ssh 1 sudo apt-get update 2 sudo apt-get install openssh-server 3 sudo ps -e |grep ssh 有sshd ...
- 【转】Ubuntu下出现Mysql error(2002)的解决方法
过了一阵子后,为了写分布式作业,重新使用Mysql时,发现虽然启动成功了,但是连接的时候去出现如下错误ERROR 2002 (HY000): Can't connect to local MySQL ...
- C#报错"线程间操作无效: 从不是创建控件“XXX”的线程访问它"--解决示例
C# Winform程序中,使用线程对界面进行更新需要特殊处理,否则会出现异常“线程间操作无效: 从不是创建控件“taskView”的线程访问它.” 在网文“http://www.cnblogs.co ...