结构
意图 将对象组合成树形结构以表示“部分-整体”的层次结构。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 ;
}
}

组合模式

最新文章

  1. python2.7安装matplotlib遇到的问题及解决方法
  2. tp框架实现防止非法登录
  3. IIS支持解析json
  4. centos安装CODEBLOCKS
  5. Leetcode Partition List
  6. 服务器端json数据文件分割合并解决方案
  7. 项目源码--Android即时通讯IM客户端
  8. c#中获取服务器IP,客户端IP以及其它
  9. PL/SQL Developer自动补全SQL技巧
  10. Junit。。。
  11. 【解决】U盘装系统(Win7/Win8)&amp; 装双系统
  12. SD/MMC卡初始化及读写流程
  13. JS 在 HTML 无缝滚动
  14. masonry使用问题
  15. 自定义七天签到View
  16. [BZOJ1007] [HNOI2008] 水平可见直线 (凸包)
  17. Android开发技巧——高亮的用户操作指南
  18. 字符串转xml,特殊字符的问题
  19. CentOS6.5 切换 图形界面 与 命令行界面
  20. 往redis中存储数据是利用pipeline方法

热门文章

  1. POJ:3320-Jessica's Reading Problem(尺取法)
  2. [BZOJ3172 ][Tjoi2013]单词(AC自动机)
  3. [Uva11178]Morley&#39;s Theorem(计算几何)
  4. 栈--数据结构与算法Javascript描述(4)
  5. js简单的获取与输出
  6. Storm: 集群安装和配置
  7. 从键盘输入数,输出它们的平方值&amp;判断是不是2的阶次方数
  8. interface in iOS
  9. 《Cracking the Coding Interview》——第1章:数组和字符串——题目7
  10. Canvas 图片平铺设置