实现要点:

1.组合模式采用树形结构来实现普遍存在的对象容器,从而将“一对多”的关系转化“一对一”的关系,使得客户代码可以一致地处理对象和对象容器,无需关心处理的是单个的对象,还是组合的对象容器。

2.将“客户代码与复杂的对象容器结构”解耦是组合模式的核心思想,解耦之后,客户代码将与纯粹的抽象接口——而非对象容器的复内部实现结构——发生依赖关系,从而更能“应对变化”。

3.组合模式中,是将“Add和Remove等和对象容器相关的方法”定义在“表示抽象对象的Component类”中,还是将其定义在“表示对象容器的Composite类”中,是一个关乎“透明性”和“安全性”的两难问题,需要仔细权衡。这里有可能违背面向对象的“单一职责原则”,但是对于这种特殊结构,这又是必须付出的代价。

4.组合模式在具体实现中,可以让父对象中的子对象反向追溯;如果父对象有频繁的遍历需求,可使用缓存技巧来改善效率。

5. 客户端尽量不要直接调用树叶类的方法,而是借助其父类(Component)的多态性完成调用,这样可以增加代码的复用性。

使用场景:

以下情况下适用组合模式:

1.你想表示对象的部分-整体层次结构。

2.你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。


  /////////////////Component.h////////////////////////
 #pragma once
class Component
{
public:
virtual ~Component();
Component();
virtual void Operation() = ;
virtual void Add( Component*);
virtual void Remove( Component*);
virtual Component* GetChild(int);
protected:
private:
};
 /////////////////Component.cpp////////////////////////
#include "Component.h"
Component::Component()
{ }
Component::~Component()
{ }
void Component::Add( Component* com)
{ } void Component::Remove( Component* com)
{ }
void Component::Operation()
{ }
Component* Component::GetChild(int index)
{
return ;
}
 /////////////////Composite.h////////////////////////
#pragma once
#include "Component.h"
#include <vector>
using namespace std;
class Composite : public Component
{
public:
Composite(const string&);
~Composite();
void Operation() ;
void Add( Component*);
void Remove( Component*);
Component* GetChild(int);
protected:
private:
vector<Component*> comVec ;
string _name ;
};
 /////////////////Composite.cpp////////////////////////
#include "Composite.h"
#include "Component.h"
#include <iostream>
#include <string>
using namespace std; Composite::Composite(const string& name)
{
_name = name ;
}
Composite::~Composite()
{ }
void Composite::Operation()
{
cout<<"Composite operation : "<<_name<<endl;
vector<Component*>::iterator iter = comVec.begin();
for (;iter != comVec.end() ; iter++)
{ if (_name == "Com2")
{
cout<<"------";
}
if (_name == "Com1")
{
cout<<"---";
} (*iter)->Operation();
} }
void Composite::Add( Component* com)
{
comVec.push_back(com);
}
void Composite::Remove( Component* com)
{
for (vector<Component*>::iterator it = comVec.begin() ; it != comVec.end() ;)
{
if (com == *it)
{
it = comVec.erase(it);
}
else
{
it++;
}
}
} Component* Composite::GetChild(int index)
{
return comVec[index] ;
}
 ////////////////////Leaf.h///////////////////////
#pragma once
#include "Component.h"
#include <iostream>
#include <string>
using namespace std;
class Leaf : public Component
{
public:
Leaf(const string&);
~Leaf();
void Operation();
protected:
private:
string _name;
}; Leaf::Leaf(const string& name)
{
_name = name ;
}
Leaf::~Leaf()
{ }
void Leaf::Operation()
{
cout<<"Leaf operation : "<< _name <<endl;
}
 /////////////////main////////////////////////////
#include "Component.h"
#include "Composite.h"
#include "Leaf.h"
#include <iostream>
#include <string>
using namespace std; int main()
{
Component* leaf1 = new Leaf("leaf1") ;
Component* leaf2 = new Leaf("leaf2") ;
Component* leaf3 = new Leaf("leaf3") ;
Component* com1 = new Composite("Com1");
com1->Add(leaf1);
com1->Add(leaf2);
com1->Add(leaf3); Component* leaf4 = new Leaf("leaf4") ;
Component* leaf5 = new Leaf("leaf5") ;
Component* com2 = new Composite("Com2");
com2->Add(leaf4);
com2->Add(leaf5); com1->Add(com2); com1->Operation(); getchar();
return ;
}

Composite 模式的实现的更多相关文章

  1. 组合模式/composite模式/对象结构型模式

    组合模式/composite模式/对象结构型 意图 将对象组合成树形结构以表示"整体--部分"的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 动机 C ...

  2. C++基础——模拟事务 (2)Composite模式

    =================================版权声明================================= 版权声明:原创文章 禁止转载  请通过右侧公告中的“联系邮 ...

  3. Composite模式

    1 意图:将对象组成树形结构,以表示“部分——整体”的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 2 动机:同意处理图元对象和包含图元的容器对象.Composite通过 ...

  4. Java 实现组合(Composite)模式

    类图 /** * 树 总体 * * @author stone * */ public class Tree { private TreeNode root; //根节点 public Tree(St ...

  5. 【结构型】Composite模式

    组合模式意在将对象组合成树形结构以表示部分与整体的层次结构关系,并且用户对单个对象的操作以有对组合对象的操作都是一致的.即:组合对象 is-a 单个对象,同时又可以组合着 n 个的单个对象(甚至于其他 ...

  6. (原创)composite模式和bridge模式是天生的好朋友

    composite模式的意图是:将对象组合成树形结构以表示“部分-整体”的层次结构.composite使得用户对单个对象和组合对象的使用具有一致性.它的类图如下: composite模式的实现分为透明 ...

  7. Java设计模式(8)组合模式(Composite模式)

    Composite定义:将对象以树形结构组织起来,以达成“部分-整体” 的层次结构,使得客户端对单个对象和组合对象的使用具有一致性. Composite比较容易理解,想到Composite就应该想到树 ...

  8. 设计模式之——Composite模式

    composite模式又叫做组合模式/复合模式. 它是一种能够使容器与内容具有一致性,创造出递归结构的模式. 示例程序是列出文件夹以及其内部文件与文件夹一览的功能: 可以由示例图看出,有一个电影文件夹 ...

  9. Composite模式 组合模式

    Android的ViewGroup 和 View 的关系,即是采用组合模式 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件 ...

  10. Composite模式(组合设计模式)

    Composite 设计模式? 在计算机的文件系统中,有"文件夹"的概念(在有些操作系统(Linux操作系统)中,也称为"目录").文件夹里面既可以放入文件,也 ...

随机推荐

  1. vs未找到导入的项目,请确认 <Import> 声明中的路径正确

    当使用vs出现下列情况: D:\xxxx\Web\Web.csproj : error  : 无法读取项目文件“Web.csproj”. D:\xxxx\WebServiceManager\Web\W ...

  2. Java---网络编程(4)-C/S-B/S

    C/S 与 B/S ☆ C/S ( Client/Server ) 客户端和服务端的特点 1.客户端和服务端的软件都需要程序员进行编写. 2.客户端维护起来较为麻烦.(缺陷) 3.客户端的存在可以将一 ...

  3. 基于Processing的数据可视化

    虽然数据可视化领域有很多成熟.界面友好.功能强大的软件产品(例如Tableau.VIDI.NodeXL等),但是借助Processing我们可以基于Java语言框架进行丰富多元的可视化编程,熟悉了Pr ...

  4. App被拒绝的原因收录

    转自:dApps开发者 » APP被苹果App Store拒绝的79个原因(未完待续) 1.程序有重大bug,程序不能启动,或者中途退出.2.绕过苹果的付费渠道,我们之前游戏里的用兑换码兑换金币.3. ...

  5. Sicily1020-大数求余算法及优化

    Github最终优化代码: https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1020.c 题目如下: 1020. ...

  6. 爬虫技术实战 | WooYun知识库

    爬虫技术实战 | WooYun知识库 爬虫技术实战 大数据分析与机器学习领域Python兵器谱-大数据邦-微头条(wtoutiao.com) 大数据分析与机器学习领域Python兵器谱

  7. bzoj4448 SCOI2015 情报传递 message

    传送门bzoj4448 题解 离线之后构建树上主席树,每个点的线段树维护到根路径的信息,不用链剖(我的链剖只是拿来求\(\mathrm{lca}\)的),时空复杂度\(O(n\log{n})\). c ...

  8. winform datagridview如何获取索引 分类: DataGridView 2014-04-11 13:42 216人阅读 评论(0) 收藏

    datagridview.CurrentCell.RowIndex;            是当前活动的单元格的行的索引 datagridview.SelectedRows  ;           ...

  9. android 小例之两列菜单关联

    因为项目需要,做了个简单的菜单关联,其实很简单 左侧是个listview 选中的时候刷新右侧关联数据,类似网易新闻选择订阅页面 这里需要注意的是 在点击完左侧菜单列表的时候 直接右侧刷新会不一定出来数 ...

  10. Unity UGUI——Rect Transform组件(基础属性)

    基础属性:Width.Height.Pivot图示 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTXJfQUhhbw==/font/5a6L5L2T/fo ...