结构型设计模式之组合模式(Composite)
| 结构 | ![]() |
| 意图 | 将对象组合成树形结构以表示“部分-整体”的层次结构。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)的更多相关文章
- 乐在其中设计模式(C#) - 组合模式(Composite Pattern)
原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...
- 【设计模式】组合模式 Composite Pattern
树形结构是软件行业很常见的一种结构,几乎随处可见, 比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...
- 二十四种设计模式:组合模式(Composite Pattern)
组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...
- python 设计模式之组合模式Composite Pattern
#引入一 文件夹对我们来说很熟悉,文件夹里面可以包含文件夹,也可以包含文件. 那么文件夹是个容器,文件夹里面的文件夹也是个容器,文件夹里面的文件是对象. 这是一个树形结构 咱们生活工作中常用的一种结构 ...
- 设计模式-12组合模式(Composite Pattern)
1.模式动机 很多时候会存在"部分-整体"的关系,例如:大学中的部门与学院.总公司中的部门与分公司.学习用品中的书与书包.在软件开发中也是这样,例如,文件系统中的文件与文件夹.窗体 ...
- 设计模式 笔记 组合模式 Composite
//---------------------------15/04/16---------------------------- //Composite 组合模式----对象结构型模式 /* 1:意 ...
- [设计模式] 8 组合模式 Composite
DP书上给出的定义:将对象组合成树形结构以表示“部分-整体”的层次结构.组合使得用户对单个对象和组合对象的使用具有一致性.注意两个字“树形”.这种树形结构在现实生活中随处可见,比如一个集团公司,它有一 ...
- 【设计模式】—— 组合模式Composite
前言:[模式总览]——————————by xingoo 模式意图 使对象组合成树形的结构.使用户对单个对象和组合对象的使用具有一致性. 应用场景 1 表示对象的 部分-整体 层次结构 2 忽略组合对 ...
- 设计模式之组合模式(Composite)
组合模式原理:组合模式的作用是讲继承同一父类的不同子类对象组合起来,形成一个树形的结构,例如公司的部门组织 代码如下 #include <iostream> #include <st ...
随机推荐
- sed命令例子详解
sed -e '/Patricia/h' -e '/Margot/x' datafile 包含Margot的行将被包含Patricia的行替换: sed -e /WE/{h;d;}' -e '/CT/ ...
- 基于itchat定制聊天机器人
#coding=utf8import requestsimport itchat #key自己到图灵注册一个 KEY = '************************************** ...
- CSAcademy Palindromic Concatenation 字符串哈希
题意: 题目链接 给出\(n\)个字符串,求有多少对\((i,j),i \neq j\)使得\(s_i\)与\(s_j\)拼起来是回文串 分析: 设\(s_i,s_j\)的长度分别为\(L_i, L_ ...
- Hbase的安装与部署(集群版)
HBase 部署与使用 部署 Zookeeper 正常部署 $ ~/modules/zookeeper-3.4.5/bin/zkServer.sh start 首先保证 Zookeeper 集群的正常 ...
- centos 6.X 关闭selinux
SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统.在这种访问控制体系的限制下,进程只能访问那 ...
- Fiddler - 使用教程
1.断点调试,修改请求表单和响应数据 命令介绍: bpu在请求开始时中断,bpafter在响应到达时中断,bps在特定http状态码时中断,bpv/bpm在特定请求method时中断. bpu m.t ...
- USACO Section1.5 Prime Palindromes 解题报告
pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- mac虚拟机上(centos系统)设置联网第二种方式
这种方式简单,不容易出错,用的桥接的方式. 这样的安装的centos会得到ip地址 然后编辑一下网卡配置,使其变为静态得ip 输入命令 # vi /etc/sysconfig/network-scri ...
- python_ 运算符与分支结构
# 运算符与分支结构 ### 运算符 - 赋值运算符 - 用'='表示,左边只能是变量. - 算术运算符 - +.-.*:加.减.乘 - /:除法运算,结果是浮点数 - //:除法运算,结果是整数 - ...
- 使用cloudbase-init初始化windows虚拟机
CloudBase-init简介 cloudbase-init 是 Windows 和其他系统的云初始化程序,可以设置主机名.创建用户.设置静态ip.设置密码等.对应的linux初始化程序是cloud ...
