实现要点:

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. rt: Unknown command 'PATH='

  2. org.springframework.web.servlet.view

    view包下面的类和接口 description:提供view和viewResolver的标准实现,也提供一些抽象基类.Spring MVC已经提供了 JSPs, Velocity, XSLT等视图的 ...

  3. ARM 开发板嵌入式linux系统与主机PC通过串口传输文件

    本文转载自http://useless20.blog.163.com/blog/static/237409982010227127576/ 嵌入式linux系统与主机通过串口传输文件 我想如果要从PC ...

  4. 利用NSIS软件制作C#安装包

    最近在做C#程序安装包,结果网上看到这个软件还是不错的,可以尝试以下. NSIS 是“Nullsoft 脚本安装系统”(Nullsoft Scriptable Installation System) ...

  5. Unity5 Screen-Space Subsurface Scattering屏幕空间次表面散射SSSSS

    用unity5自带的standard shader        ------------by wolf96 

  6. Quartus中例化工程

    一般的例化工程,需要将要例化的对象的硬件语言放入到当前工程中,比如A要例化B,需要将B的编程文件加入当前工程进来. 还有一种方法不用这么麻烦,A工程用要例化B时,在library添加B的工程路径,就可 ...

  7. ACM1997_汉诺栽塔VII

    #include <stdio.h> #include <iostream> #include <queue> #include <stdlib.h> ...

  8. Delphi:窗体自适应屏幕分辨率的改进

    在窗体依据屏幕分辨率自适应调整尺度方面,昨天的工作可以说是一个突破点.昨天的工作找到了长期以来我的原有方案的问题所在,这是非常关键的.但是昨天晚上的解决方案并不完美,今天的这个才是比较完美的解决版. ...

  9. Eclipse Maven Project

    http://www.cnblogs.com/candle806/p/3439469.html

  10. poj 3678 Katu Puzzle(Two Sat)

    题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...