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

结构图:

 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. Redis系列整理

    0.Redis系列-安装部署维护篇 1.Redis系列-远程连接redis并给redis加锁 2.Redis系列-存储篇string主要操作函数小结 3.Redis系列-存储篇list主要操作函数小结 ...

  2. 【Android】Fragment如何获取子Fragment

    今天搞了个嵌套的Fragment,通过外部的Fragment获取的子Fragment代码: this.navigationBar = (HXKJCargoNavigationView) getFrag ...

  3. 用JQuery实现表格隔行变色和突出显示当前行

    用JQuery实现表格隔行变色和突出显示当前行 上源码 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "htt ...

  4. Stat

    Description 请你编程实现一个简单(渣渣)的文本编辑器,具体要求是:给定一个单词,请你输出它在给定的文章中出现的次数和第一次出现的位置.注意:匹配单词时,不区分大小写,但要求完全匹配,即给定 ...

  5. N!

    #include<stdio.h>main(){int jiech(int n);int n;printf("n:\n");scanf("%d",& ...

  6. ajax中返回包含html的奇葩问题

    如果通过ajax返回一串包含有html标签的字符串,然后添加到相应的html文档位置,如果标签外含有空格或者换行等就会报错.

  7. Flink Program Guide (10) -- Savepoints (DataStream API编程指导 -- For Java)

    Savepoint 本文翻译自文档Streaming Guide / Savepoints ------------------------------------------------------ ...

  8. mac + apache + php

    1: 设置下用sublimetext为默认打开方式, 确保下载sublimetext 2:设置下默认打开方式为sm sudo ln -s /Applications/Sublime\ Text\ 2. ...

  9. CSS自学笔记(10):CSS3盒子模型

    CSS3为CSS技术的升级版本.最新版本. 就CSS而言,它是一个模块,是一个庞大而又复杂的模块,但是在CSS3中,将这一个庞大的模块分解为一个个容易理解的同时又很精简的小模块,同时CSS3中又添加了 ...

  10. 为每个页面加上Session判断 转

    首先新建一个类,继承自System.Web.UI.Page,然后重写OnInit,如下:   using System; using System.Data; using System.Configu ...