实现要点:

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

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

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

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

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

使用场景:

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

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

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

  1.  
  1. /////////////////Component.h////////////////////////
  1. #pragma once
  2. class Component
  3. {
  4. public:
  5. virtual ~Component();
  6. Component();
  7. virtual void Operation() = ;
  8. virtual void Add( Component*);
  9. virtual void Remove( Component*);
  10. virtual Component* GetChild(int);
  11. protected:
  12. private:
  13. };
  1. /////////////////Component.cpp////////////////////////
  2. #include "Component.h"
  3. Component::Component()
  4. {
  5.  
  6. }
  7. Component::~Component()
  8. {
  9.  
  10. }
  11. void Component::Add( Component* com)
  12. {
  13.  
  14. }
  15.  
  16. void Component::Remove( Component* com)
  17. {
  18.  
  19. }
  20. void Component::Operation()
  21. {
  22.  
  23. }
  24. Component* Component::GetChild(int index)
  25. {
  26. return ;
  27. }
  1. /////////////////Composite.h////////////////////////
  2. #pragma once
  3. #include "Component.h"
  4. #include <vector>
  5. using namespace std;
  6. class Composite : public Component
  7. {
  8. public:
  9. Composite(const string&);
  10. ~Composite();
  11. void Operation() ;
  12. void Add( Component*);
  13. void Remove( Component*);
  14. Component* GetChild(int);
  15. protected:
  16. private:
  17. vector<Component*> comVec ;
  18. string _name ;
  19. };
  1. /////////////////Composite.cpp////////////////////////
  2. #include "Composite.h"
  3. #include "Component.h"
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7.  
  8. Composite::Composite(const string& name)
  9. {
  10. _name = name ;
  11. }
  12. Composite::~Composite()
  13. {
  14.  
  15. }
  16. void Composite::Operation()
  17. {
  18. cout<<"Composite operation : "<<_name<<endl;
  19. vector<Component*>::iterator iter = comVec.begin();
  20. for (;iter != comVec.end() ; iter++)
  21. {
  22.  
  23. if (_name == "Com2")
  24. {
  25. cout<<"------";
  26. }
  27. if (_name == "Com1")
  28. {
  29. cout<<"---";
  30. }
  31.  
  32. (*iter)->Operation();
  33. }
  34.  
  35. }
  36. void Composite::Add( Component* com)
  37. {
  38. comVec.push_back(com);
  39. }
  40. void Composite::Remove( Component* com)
  41. {
  42. for (vector<Component*>::iterator it = comVec.begin() ; it != comVec.end() ;)
  43. {
  44. if (com == *it)
  45. {
  46. it = comVec.erase(it);
  47. }
  48. else
  49. {
  50. it++;
  51. }
  52. }
  53. }
  54.  
  55. Component* Composite::GetChild(int index)
  56. {
  57. return comVec[index] ;
  58. }
  1. ////////////////////Leaf.h///////////////////////
  2. #pragma once
  3. #include "Component.h"
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7. class Leaf : public Component
  8. {
  9. public:
  10. Leaf(const string&);
  11. ~Leaf();
  12. void Operation();
  13. protected:
  14. private:
  15. string _name;
  16. };
  17.  
  18. Leaf::Leaf(const string& name)
  19. {
  20. _name = name ;
  21. }
  22. Leaf::~Leaf()
  23. {
  24.  
  25. }
  26. void Leaf::Operation()
  27. {
  28. cout<<"Leaf operation : "<< _name <<endl;
  29. }
  1. /////////////////main////////////////////////////
  2. #include "Component.h"
  3. #include "Composite.h"
  4. #include "Leaf.h"
  5. #include <iostream>
  6. #include <string>
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. Component* leaf1 = new Leaf("leaf1") ;
  12. Component* leaf2 = new Leaf("leaf2") ;
  13. Component* leaf3 = new Leaf("leaf3") ;
  14. Component* com1 = new Composite("Com1");
  15. com1->Add(leaf1);
  16. com1->Add(leaf2);
  17. com1->Add(leaf3);
  18.  
  19. Component* leaf4 = new Leaf("leaf4") ;
  20. Component* leaf5 = new Leaf("leaf5") ;
  21. Component* com2 = new Composite("Com2");
  22. com2->Add(leaf4);
  23. com2->Add(leaf5);
  24.  
  25. com1->Add(com2);
  26.  
  27. com1->Operation();
  28.  
  29. getchar();
  30. return ;
  31. }

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. java---Unicode-字符转换器

    实现一个字符(包括汉字)的简单互相转换: package cn.hncu.gui2; import java.awt.Button; import java.awt.Color; import jav ...

  2. 4种字符串匹配算法:KMP(下)

    回顾:4种字符串匹配算法:BS朴素 Rabin-karp(上) 4种字符串匹配算法:有限自动机(中) 1.图解 KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R ...

  3. ubuntu制作usb启动盘

    准备: u盘 iso镜像文件--ubuntu-12.04.2-desktop-amd64.iso 烧盘软件--unetbootin-linux-583 步骤: 格式化u盘 查看u盘信息 #mount/ ...

  4. hdu 4474 大整数取模+bfs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474 (a*10+b)%c = ((a%c)*10+b%c)%c; 然后从高位开始枚举能填的数字填充, ...

  5. 真机测试---iOS证书(.p12)和描述文件(.mobileprovision)

    iOS证书和描述文件: 证书类型 使用场景 开发(Development)证书和描述文件 用于开发测试,在starain中打包后可在真机环境通过Safari调试 发布(Distribution)证书和 ...

  6. [置顶] [MATLAB技术贴]漫谈MATLAB矩阵转置

    矩阵转置是matlab最基本的操作了,但这个基本操作,也是很多初学者容易出现问题的地方.本帖通过几个实例演示matlab矩阵转置的操作. 方法一:'  运算符与  .'  运算符 >>a ...

  7. db2 alter table 语法

    DB2 alter:add/delete/reset   column 1.添加字段 alter table [table_name] add [column_name] [column_type] ...

  8. C# ToString格式大全

      C# 货币  2.5.ToString("C"); // ¥2.50 // D 10进制数 25.ToString("D5"); // 25000 // E ...

  9. Windows Live Writer的Markdown插件MarkdownInLiveWriter支持语法高亮了

    我前几天开发的Windows Live Writer的Markdown的插件MarkdownInLiveWriter支持语法高亮了.参见下图: 基本上就是把我的另一个插件CodeInLiveWrite ...

  10. 开发库比较(3) - Mobile Web 开发 - Sencha, jquerymobiel, phonejs, jqtouch, jqmobi

    我们一直坚信Html/css在界面上最终会一统江湖,因为在众多的界面编写中,qt,gtk,wpf,win form, wxwidgets等等,只有Html/CSS是真正拥有统一标准,只有这个有潜力作用 ...