实现要点:

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. 【转】Android--UI之ProgressBar--不错

    原文网址:http://www.cnblogs.com/plokmju/p/android_progressbar.html 前言 开门见山,开篇明意.这篇博客主要讲解一下Android中Progre ...

  2. 前端程序员:月薪 5K 到 5 万,我干了啥

    高贵的前端程序猿们: 如何在前端开发这种高精尖的技术领域找到心仪的工作?实现在咖啡馆喝喝咖啡敲敲代码就能升职加薪.买房买车.迎娶白富美走上人生巅峰的职业梦想?这篇<进化论:从 0 到 100,前 ...

  3. JavaScript高级程序设计11.pdf

    与操作字符串有关的最后一个方法是localeCompare(),这个方法比较两个字符串,按照字符串的字母表中的位置分别返回-1,0,1 var stringValue="yellow&quo ...

  4. t

    http://www.cnblogs.com/courtier/p/4287177.html 360导航_新一代安全上网导航 http://www.cnblogs.com/zhenzi/p/42926 ...

  5. 贪心+容器 hdu4268

    Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...

  6. ELK监控系统nginx / mysql慢日志

    ELK监控系统nginx / mysql慢日志 elasticsearch logstash kibana ELK监控系统nginx日志 1.环境准备 centos6.8_64 mini IP:192 ...

  7. 【设计模式 - 4】之原型模式(Prototype)

    1      模式简介 原型模式的定义:通过复制一个现有的对象(原型)来得到一个相似的对象. 原型模式的UML图如下图所示: 从上图中可以看到,所有的对象实体类都是继承自一个Prototype的父类, ...

  8. LINUX内核调度器+linux 内存

    http://www.cnblogs.com/tolimit/p/4303052.html

  9. Objective-C--@property,@synthesize关键字介绍

    Objective-C–@property,@synthesize关键字介绍 转载:http://www.cnblogs.com/QM80/p/3576282.html /** 注意:由@proper ...

  10. Objective-C中的@Property详解

    Objective-C中的@Property详解 @Property (属性) class vairs 这个属性有nonatomic, strong, weak, retain, copy等等 我把它 ...