结构型设计模式之组合模式(Composite)
2024-08-29 12:02:29
结构 | |
意图 | 将对象组合成树形结构以表示“部分-整体”的层次结构。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 ;
}
}
组合模式
最新文章
- python2.7安装matplotlib遇到的问题及解决方法
- tp框架实现防止非法登录
- IIS支持解析json
- centos安装CODEBLOCKS
- Leetcode Partition List
- 服务器端json数据文件分割合并解决方案
- 项目源码--Android即时通讯IM客户端
- c#中获取服务器IP,客户端IP以及其它
- PL/SQL Developer自动补全SQL技巧
- Junit。。。
- 【解决】U盘装系统(Win7/Win8)&; 装双系统
- SD/MMC卡初始化及读写流程
- JS 在 HTML 无缝滚动
- masonry使用问题
- 自定义七天签到View
- [BZOJ1007] [HNOI2008] 水平可见直线 (凸包)
- Android开发技巧——高亮的用户操作指南
- 字符串转xml,特殊字符的问题
- CentOS6.5 切换 图形界面 与 命令行界面
- 往redis中存储数据是利用pipeline方法
热门文章
- POJ:3320-Jessica's Reading Problem(尺取法)
- [BZOJ3172 ][Tjoi2013]单词(AC自动机)
- [Uva11178]Morley&#39;s Theorem(计算几何)
- 栈--数据结构与算法Javascript描述(4)
- js简单的获取与输出
- Storm: 集群安装和配置
- 从键盘输入数,输出它们的平方值&;判断是不是2的阶次方数
- interface in iOS
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目7
- Canvas 图片平铺设置