一、产生背景

又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。

这种模式创建了一个包含自己对象组的类。该类提供了修改相同对象组的方式。

我们通过下面的实例来演示组合模式的用法。实例演示了一个组织中员工的层次结构。

二、通常做法

组合模式参与者:

  ◊ Component

    ° 声明组合中对象的接口;

    ° 实现全部类中公共接口的默认行为;

    ° 声明访问和管理子类的接口;

    ° (可选择)定义接口提供在递归结构中访问父类。

  ◊ Leaf

    ° 表示在组合对象中叶子节点对象,没有子节点;

    ° 定义组合对象中的初始行为。

  ◊ Composite

    ° 定义Component子类的行为;

    ° 保存Component子类;

    ° 实现Component接口的子类关联操作。

  ◊ Client

    ° 通过Component接口组合多个对象。

三、实例

1、component.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CompositionPattern
  8. {
  9. public abstract class Component
  10. {
  11. protected string _name;
  12.  
  13. public Component(string name)
  14. {
  15. _name = name;
  16. }
  17.  
  18. public abstract void Add(Component c);
  19.  
  20. public abstract void Remove(Component c);
  21.  
  22. public abstract void Display(int depth);
  23.  
  24. }
  25. }

2、composite.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CompositionPattern
  8. {
  9. public class Composite : Component
  10. {
  11. private List<Component> _children = new List<Component>();
  12.  
  13. public Composite(string name)
  14. : base(name)
  15. {
  16. }
  17.  
  18. public override void Add(Component component)
  19. {
  20. _children.Add(component);
  21. }
  22.  
  23. public override void Remove(Component component)
  24. {
  25. _children.Remove(component);
  26. }
  27.  
  28. public override void Display(int depth)
  29. {
  30. Console.WriteLine(new String('-', depth) + _name);
  31.  
  32. foreach (Component component in _children)
  33. {
  34. component.Display(depth + );
  35. }
  36. }
  37. }
  38. }

3、leaf.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CompositionPattern
  8. {
  9. public class Leaf:Component
  10. {
  11. public Leaf(string name) : base(name) { }
  12.  
  13. public override void Add(Component c)
  14. {
  15. Console.WriteLine("Cannot add to a");
  16. }
  17.  
  18. public override void Remove(Component c)
  19. {
  20. Console.WriteLine("Cannot remove from a leaf");
  21. }
  22.  
  23. public override void Display(int depth)
  24. {
  25. Console.WriteLine(new String('-', depth) + _name);
  26. }
  27. }
  28. }

4、客户端

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CompositionPattern
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. // Create a tree structure
  14. Composite root = new Composite("root");
  15. root.Add(new Leaf("Leaf A"));
  16. root.Add(new Leaf("Leaf B"));
  17.  
  18. Composite comp = new Composite("Composite X");
  19. comp.Add(new Leaf("Leaf XA"));
  20. comp.Add(new Leaf("Leaf XB"));
  21.  
  22. root.Add(comp);
  23. root.Add(new Leaf("Leaf C"));
  24.  
  25. // Add and remove a leaf
  26. Leaf leaf = new Leaf("Leaf D");
  27. root.Add(leaf);
  28. root.Remove(leaf);
  29.  
  30. // Recursively display tree
  31. root.Display();
  32.  
  33. Console.ReadLine();
  34.  
  35. }
  36. }
  37. }

四、设计模式分析

  组合模式可以适用以下情形:

  ◊ 希望把对象表示成部分—整体层次结构;

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

  组合模式具有以下特点:

  ◊ 定义了包含基本对象和组合对象的类层次结构。基本对象可以被组合成更复杂的组合对象,而这个组合对象又可以被组合,不断的递归下去。客户代码中,任何用到基本对象的地方都可以使用组合对象;

  ◊ 简化客户代码。客户可以一致地使用组合结构和单个对象。这样用户就不必关心处理的是一个叶子节点还是一个组合组件,从而简化了客户代码;

  ◊ 使得新增类型的组件更加容易。新定义的Composite或Leaf子类自动地与已有的结构和客户代码一起协同工作,客户程序不需因新的Component类而改变。

设计模式のCompositePattern(组合模式)----结构模式的更多相关文章

  1. 十二、享元(Flyweight)模式--结构模式(Structural Pattern)

    Flyweight在拳击比赛中指最轻量级,即"蝇量级",有些作者翻译为"羽量级".这里使用"享元 模式"更能反映模式的用意. 享元模式以共享 ...

  2. 九、 合成(Composite)模式 --结构模式(Structural Pattern)

    合成模式:有时又叫做部分-整体模式(Part-Whole).合成模式将对象组织到树结构中,可以用来描述整体与部分的关系.合成模式可以使客户端将单纯元素与复合元素同等看待. 合成模式分为安全式和透明式 ...

  3. 十、装饰(Decorator)模式 --结构模式(Structural Pattern)

    装饰(Decorator)模式又名包装(Wrapper)模式[GOF95].装饰模式以对客户端透明的方 式扩展对象的功能,是继承关系的一个替代方案. 装饰模式类图: 类图说明: 抽象构件(Compon ...

  4. 七、适配器(Adapter)模式--结构模式(Structural Pattern)

    适配器模式:把一个类的接口变换成客户端所期待的另一种接口,从而使原本接口不匹配而无法在一起工作的两个类能够在一起工作. 类的 Adapter模式的结构: 类适配器类图: 由图中可以看出,Adaptee ...

  5. 十一、外观(Facade)模式--结构模式(Structural Pattern)

    外部与一个子系统的通信必须通过一个统一的门面(Facade)对象进行,这就是门面模式.门面模式要求一个子系统的外部与其内部的通信必须通过一个统一的门面(Facade)对象进行. 门面模式提供一个高层次 ...

  6. 八、桥接模式--结构模式(Structural Pattern)

    桥梁模式:将抽象化(Abstraction)与实现化 (Implementation)脱耦,使得二者可以独立地变化. 桥梁模式类图: 抽象化(Abstraction)角色:抽象化给出的定义,并保存 一 ...

  7. C#设计模式(10)——组合模式(Composite Pattern)

    一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...

  8. 设计模式:组合模式(Composite)

    定   义:将对象组合树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象使用具有一致性. 结构图: Component类: abstract class Component ...

  9. [JAVA设计模式]第三部分:结构模式

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

随机推荐

  1. JavaWeb学习日记----DTD

    DTD:文档类型定义,可以定义合法的XML文档构建模块.使用一系列的合法标签元素来定义文档的结构. 现有一个XML文档内容如下: <?xml version="1.0"?&g ...

  2. MongoDB日期类型查询

    一.前言 MongoDB 里面的日期类型是没有时区概念的,默认存储的是 ISODate("2018-04-02T13:19:16.418Z") 这种格式的零时区时间,比北京时间晚了 ...

  3. vue2+webpack 移动生态 常用依赖

    1.脚手架:官方的vue-cli已经很方便了 2.路由:vue-router : https://router.vuejs.org/zh-cn/essentials/named-routes.html ...

  4. angular select2 下拉单选和多选的取值赋值

    官网:http://select2.github.io/examples.html 兼容性: 引入文件 /select2.min.js /select2.min.css html <select ...

  5. HTML基础总结

    HTML细化知识点总结 1.h1-h6标签 都是标题标签,定义一段话的标题,h1最大,依次递减,h6最小 标题标签的作用:让文本加粗显示 2. 段落标签:p标签 用来显示一段文本(图片),它会忽略源代 ...

  6. POJ 1704 Georgia and Bob(阶梯Nim博弈)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11357   Accepted: 3749 Description Geor ...

  7. Linux 安装 jdk8

    切换目录 cd /usr 创建目录 mkdir java cd java 下载 jdk rz 或者 ftp 都行,只要能成功上传 解压 tar zxvf jdk-8u181-linux-x64.tar ...

  8. C# SharpMap的简单使用

    本文是利用ShapMap实现GIS的简单应用的小例子,以供学习分享使用.关于SharpMap的说明,网上大多是以ShapeFile为例进行简单的说明,就连官网上的例子也不多.本文是自己参考了源代码进行 ...

  9. C# 利用Log4Net进行日志记录

    概述 本文主要简单说明如何使用Log4Net进行日志记录,在程序开发过程中记录日志的优点: 它可以提供应用程序运行时的精确环境,可供开发人员尽快找到应用程序中的Bug: 一旦在程序中加入了Log 输出 ...

  10. Floyd算法_MATLAB

    %求图中任意两点之间的最短距离与最短路径 %floyd算法通用程序,输入a为赋权邻接矩阵 %输出为距离矩阵D,和最短路径矩阵path function D=floyd(a) n=size(a,);%行 ...