组合模式:将对象组合成树形结构以表示“部分-真题”的结构层次。组合模式使得用户对单个对象和组合对象的使用具有一致性。

结构图:

 using System;
using System.Collections.Generic;
using System.Text; namespace 组合模式
{
class Program
{
static void Main(string[] args)
{
Composite root = new Composite("root");
root.Add(new Leaf("Leaf A"));
root.Add(new Leaf("Leaf B")); Composite comp = new Composite("Composite X");
comp.Add(new Leaf("Leaf XA"));
comp.Add(new Leaf("Leaf XB")); root.Add(comp); Composite comp2 = new Composite("Composite XY");
comp2.Add(new Leaf("Leaf XYA"));
comp2.Add(new Leaf("Leaf XYB")); comp.Add(comp2); root.Add(new Leaf("Leaf C")); Leaf leaf = new Leaf("Leaf D");
root.Add(leaf);
root.Remove(leaf); root.Display(); Console.Read();
}
} abstract class Component
{
protected string name; public Component(string name)
{
this.name = name;
} public abstract void Add(Component c);
public abstract void Remove(Component c);
public abstract void Display(int depth);
} class Composite : Component
{
private List<Component> children = new List<Component>(); public Composite(string name)
: base(name)
{ } public override void Add(Component c)
{
children.Add(c);
} public override void Remove(Component c)
{
children.Remove(c);
} public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + name); foreach (Component component in children)
{
component.Display(depth + );
}
}
} class Leaf : Component
{
public Leaf(string name)
: base(name)
{ } public override void Add(Component c)
{
Console.WriteLine("Cannot add to a leaf");
} public override void Remove(Component c)
{
Console.WriteLine("Cannot remove from a leaf");
} public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + name);
}
}
}

Composite

当你发现需求中是体现部分与整体的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中的多有对象时,就应该考虑用组合模式。

GoF——组合模式的更多相关文章

  1. 《JAVA与模式》之组合模式

    定义(GoF<设计模式>): 将对象组合成树形结构以表示“部分整体”的层次结构.组合模式使得用户对单个对象和使用具有一致性. 及角色: 1.Component 是组合中的对象声明接口,在适 ...

  2. .NET设计模式(11):组合模式(Composite Pattern)(转)

    概述 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦. 意图 将对 ...

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

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

  4. 第9章 组合模式(Composite Pattern)

    原文 第9章 组合模式(Composite Pattern) 概述: 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理 ...

  5. 设计模式组合模式(Composite)精华

    23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例,他们帮助如何创建一个系统独立.这是一个这些对象和陈述的组合. 创建使用继承类的类架构更改实例.的对象类型模型的建 ...

  6. C#设计模式之十组合模式(Composite)【结构型】

    一.引言   今天我们要讲[结构型]设计模式的第四个模式,该模式是[组合模式],英文名称是:Composite Pattern.当我们谈到这个模式的时候,有一个物件和这个模式很像,也符合这个模式要表达 ...

  7. 【Unity3D与23种设计模式】组合模式(Composite)

    前段时间在忙一个新项目 博客好久没有更新了 GoF中定义: "将对象以树状结构组合,用以表现部分-全体的层次关系.组合模式让客户端在操作各个对象或组合时是一致的." 是一致的意思就 ...

  8. C++设计模式——组合模式

    问题描述 上图,是一个公司的组织结构图,总部下面有多个子公司,同时总部也有各个部门,子公司下面有多个部门.如果对这样的公司开发一个OA系统,作为程序员的你,如何设计这个OA系统呢?先不说如何设计实现, ...

  9. C#设计模式之九组合模式(Composite Pattern)【结构型】

    一.引言 今天我们要讲[结构型]设计模式的第四个模式,该模式是[组合模式],英文名称是:Composite Pattern.当我们谈到这个模式的时候,有一个物件和这个模式很像,也符合这个模式要表达的意 ...

随机推荐

  1. Java学习—— for循环

    For双重循环 /* 循环语句嵌套 */ class ForForTest { public static void main(String[] args) { /*int x,y = 0; for( ...

  2. jvm如何知道那些对象需要回收

    1 首先的问题是:jvm如何知道那些对象需要回收 ? 目前有两种算法 引用计数法 每个对象上都有一个引用计数,对象每被引用一次,引用计数器就+1,对象引用被释放,引用计数器-1,直到对象的引用计数为0 ...

  3. shell编程001

    1.shell中如何进行算术计算   A=1; B=2 (1)let C=$A+$B (2)C=$[$A+$B] (3)C=$(($A+$B)) (4)C=`expr $A + $B` (注意运算符前 ...

  4. 点击后改变css属性

    在html中: <div class="formbuilder">    <div class="active">Heading< ...

  5. webstrom 常用快捷键

    最近在学习javascript,同时发现了一款非常好用的IDE webstrom 现在记录改IDE的快捷键 1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任意目录的文 ...

  6. JDK Debug

    http://ishare.iask.sina.com.cn/f/23897007.html http://hi.baidu.com/bd_hare/item/7edd0415b60f0101e65c ...

  7. node.js介绍

    官网说明: Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable ...

  8. matlab中文论坛视频谷普教程MATLAB压缩包介绍

    matlab中文论坛视频谷普教程MATLAB压缩包介绍 我也正在学习这个软件 ,看到这个教程就在这里分享了,希望大家喜欢!Matlab 初学者视频教学1. Matlab视频:Matlab中文论坛为新手 ...

  9. JQuery(下)

    26.jQuery 中的 DOM 操作 )DOM(Document Object Model—文档对象模型):一种与浏览器, 平台, 语言无关的接口, 使用该接口可以轻松地访问页面中所有的标准组件 ) ...

  10. 利用PowerDesigner15在win7系统下对MySQL 进行反向project(二)

    利用PowerDesigner15在win7系统下对MySQL 进行反向project 1.打开PowerDesigner,建立新模型.选择Physical Data Model中的Physical ...