结构
意图 将对象组合成树形结构以表示“部分-整体”的层次结构。C o m p o s i t e 使得用户对单个对象和组合对象的使用具有一致性。
适用性
  • 你想表示对象的部分-整体层次结构。
  • 你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。
  using System;
using System.Collections; abstract class Component
{
protected string strName; public Component(string name)
{
strName = name;
} abstract public void Add(Component c); public abstract void DumpContents(); // other operations for delete, get, etc.
} class Composite : Component
{
private ArrayList ComponentList = new ArrayList(); public Composite(string s) : base(s) {} override public void Add(Component c)
{
ComponentList.Add(c);
} public override void DumpContents()
{
// First dump the name of this composite node
Console.WriteLine("Node: {0}", strName); // Then loop through children, and get then to dump their contents
foreach (Component c in ComponentList)
{
c.DumpContents();
}
}
} class Leaf : Component
{
public Leaf(string s) : base(s) {} override public void Add(Component c)
{
Console.WriteLine("Cannot add to a leaf");
} public override void DumpContents()
{
Console.WriteLine("Node: {0}", strName);
}
} /// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
Component SetupTree()
{
// here we have to create a tree structure,
// consisting of composites and leafs.
Composite root = new Composite("root-composite");
Composite parentcomposite;
Composite composite;
Leaf leaf; parentcomposite = root;
composite = new Composite("first level - first sibling - composite");
parentcomposite.Add(composite);
leaf = new Leaf("first level - second sibling - leaf");
parentcomposite.Add(leaf);
parentcomposite = composite;
composite = new Composite("second level - first sibling - composite");
parentcomposite.Add(composite);
composite = new Composite("second level - second sibling - composite");
parentcomposite.Add(composite); // we will leaf the second level - first sibling empty, and start
// populating the second level - second sibling
parentcomposite = composite;
leaf = new Leaf("third level - first sibling - leaf");
parentcomposite.Add(leaf); leaf = new Leaf("third level - second sibling - leaf");
parentcomposite.Add(leaf);
composite = new Composite("third level - third sibling - composite");
parentcomposite.Add(composite); return root;
} public static int Main(string[] args)
{
Component component;
Client c = new Client();
component = c.SetupTree(); component.DumpContents();
return ;
}
}

组合模式

结构型设计模式之组合模式(Composite)的更多相关文章

  1. 乐在其中设计模式(C#) - 组合模式(Composite Pattern)

    原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...

  2. 【设计模式】组合模式 Composite Pattern

    树形结构是软件行业很常见的一种结构,几乎随处可见,  比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...

  3. 二十四种设计模式:组合模式(Composite Pattern)

    组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...

  4. python 设计模式之组合模式Composite Pattern

    #引入一 文件夹对我们来说很熟悉,文件夹里面可以包含文件夹,也可以包含文件. 那么文件夹是个容器,文件夹里面的文件夹也是个容器,文件夹里面的文件是对象. 这是一个树形结构 咱们生活工作中常用的一种结构 ...

  5. 设计模式-12组合模式(Composite Pattern)

    1.模式动机 很多时候会存在"部分-整体"的关系,例如:大学中的部门与学院.总公司中的部门与分公司.学习用品中的书与书包.在软件开发中也是这样,例如,文件系统中的文件与文件夹.窗体 ...

  6. 设计模式 笔记 组合模式 Composite

    //---------------------------15/04/16---------------------------- //Composite 组合模式----对象结构型模式 /* 1:意 ...

  7. [设计模式] 8 组合模式 Composite

    DP书上给出的定义:将对象组合成树形结构以表示“部分-整体”的层次结构.组合使得用户对单个对象和组合对象的使用具有一致性.注意两个字“树形”.这种树形结构在现实生活中随处可见,比如一个集团公司,它有一 ...

  8. 【设计模式】—— 组合模式Composite

    前言:[模式总览]——————————by xingoo 模式意图 使对象组合成树形的结构.使用户对单个对象和组合对象的使用具有一致性. 应用场景 1 表示对象的 部分-整体 层次结构 2 忽略组合对 ...

  9. 设计模式之组合模式(Composite)

    组合模式原理:组合模式的作用是讲继承同一父类的不同子类对象组合起来,形成一个树形的结构,例如公司的部门组织 代码如下 #include <iostream> #include <st ...

随机推荐

  1. iOS-delegate设计模式

    1. 使用场合 1> A想让B帮忙做一些事情,就让B成为A的代理 2> A想通知一下B发生了某些事情,或者想传递一些数据给B,就让B成为A的代理 3> B想监听A所做的一些事情, 就 ...

  2. POJ 2676 数独(DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21612   Accepted: 10274   Specia ...

  3. C语言进阶——分支语句06

    if分支语句分析: if语句用于根据条件选择执行语句 else不能独立存在且总是与在它之前的最近if相匹配 esle语句后可以连接其他if语句 用法如下: if(condition) { //stat ...

  4. 移动端的拖拽排序在react中实现 了解一下

    最近做一个拖拽排序的功能找了好几个有一个步骤简单,结合redux最好不过了,话不多说上代码 第一步: npm install react-draggable-tags --save 第二步 sort. ...

  5. 3336 /P1948电话网络(二分答案)

    3336 电话网络  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold       题目描述 Description 由于地震使得连接汶川县城电话线全部损坏,假如你是 ...

  6. 图解java面试

    图解Java面试题:基本语法 2017-02-07 14:34 出处:清屏网 人气:178 评论(0)   内容大纲.png &和&&的区别 &和&&的 ...

  7. VSX-3 VSCT文件

    关于VSPackage中的VSCT,算是VSX开发中比较重要的一个成员. 我这里给出LearnVSXNow!系列文章关于VSCT的链接,除了#14有译文. #14 #18 #25 看完上面几篇文章,也 ...

  8. 【Python】python模块加载

    一个python文件就是一个模块 标准模块 python自带的模块就是标准模块,也就说可以直接import进来的就是标准模块 import datetime import random 第三方模块 别 ...

  9. Jmeter微信小程序接口测试

    最近公司新项目组开发一款微信小程序电商平台,为了更好保证产品质量,因此提出了需要进行接口测试. 从接口本身来讲,对其测试与其他项目应该是一样的.所以不难理解,我们要对小程序的接口测试需要准备的 材料有 ...

  10. 容器基础(一): Docker介绍

    IaaS IaaS阶段, 用户租借基础设施,但是还是需要像以前管理服务器那样,用脚本或者手工方式在这些机器上部署应用.这个过程中当然难免会碰到云端机器和本地机器环境不一致的问题.想想每一次同步不同机器 ...