1、组合模式简介

1.1>、定义

  组合模式主要用来处理一类具有“容器特征”的对象——即它们在充当对象的同时,又可以作为容器包含其他多个对象。

1.2>、使用频率

   中高

2、组合模式结构图

2.1>、结构图

2.2>、参与者

  组合模式参与者:

  ◊ Component

    ° 声明组合中对象的接口;

    ° 实现全部类中公共接口的默认行为;

    ° 声明访问和管理子类的接口;

    ° (可选择)定义接口提供在递归结构中访问父类。

  ◊ Leaf

    ° 表示在组合对象中叶子节点对象,没有子节点;

    ° 定义组合对象中的初始行为。

  ◊ Composite

    ° 定义Component子类的行为;

    ° 保存Component子类;

    ° 实现Component接口的子类关联操作。

  ◊ Client

    ° 通过Component接口组合多个对象。

3、组合模式结构实现

  Component.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.CompositePattern.Structural
{
public 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);
}
}

  Leaf.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.CompositePattern.Structural
{
public 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.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.CompositePattern.Structural
{
public class Composite : Component
{
private List<Component> _children = new List<Component>(); public Composite(string name)
: base(name)
{
} public override void Add(Component component)
{
_children.Add(component);
} public override void Remove(Component component)
{
_children.Remove(component);
} public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + _name); foreach (Component component in _children)
{
component.Display(depth + );
}
}
}
}

  Client.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.CompositePattern.Structural
{
public class Client
{
static void Main(string[] args)
{
// Create a tree structure
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);
root.Add(new Leaf("Leaf C")); // Add and remove a leaf
Leaf leaf = new Leaf("Leaf D");
root.Add(leaf);
root.Remove(leaf); // Recursively display tree
root.Display();
}
}
}

  运行结果:

-root
---Leaf A
---Leaf B
---Composite X
-----Leaf XA
-----Leaf XB
---Leaf C
请按任意键继续. . .

4、组合模式实践应用

  Shape.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.CompositePattern.Practical
{
public abstract class Shape
{
protected string _name; public Shape(string name)
{
this._name = name;
} /// <summary>
/// 面积
/// </summary>
/// <returns></returns>
public abstract double Area(); /// <summary>
/// 显示
/// </summary>
public abstract void Display();
}
}

  Circle.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.CompositePattern.Practical
{
/// <summary>
/// 圆形
/// </summary>
public class Circle : Shape
{
private double _radius; public Circle(string name, double radius)
: base(name)
{
this._radius = radius;
} public override double Area()
{
return Math.Round(Math.PI * _radius * _radius, );
} public override void Display()
{
Console.WriteLine("{0} 半径:{1},面积:{2}", _name, _radius, this.Area());
}
}
}

  Rectangle.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.CompositePattern.Practical
{
/// <summary>
/// 矩形
/// </summary>
public class Rectangle : Shape
{
private double _width;
private double _height; public Rectangle(string name, double width, double height)
: base(name)
{
this._width = width;
this._height = height;
} public override double Area()
{
return _width * _height;
} public override void Display()
{
Console.WriteLine("{0} 长:{1},宽:{2},面积:{3}", _name, _width, _height, this.Area());
}
}
}

  Triangle.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.CompositePattern.Practical
{
/// <summary>
/// 三角形
/// </summary>
public class Triangle : Shape
{
private double _a;
private double _b;
private double _c; /// <summary>
/// 三角形构造函数
/// 参数:三角形名称和三条边长
/// </summary>
/// <param name="name">三角形名称</param>
/// <param name="a">第一条边长</param>
/// <param name="b">第二条边长</param>
/// <param name="c">第三条边长</param>
public Triangle(string name, double a, double b, double c)
: base(name)
{
_a = a;
_b = b;
_c = c;
} public override double Area()
{
double p = (_a + _b + _c) / ;
return Math.Sqrt(p * (p - _a) * (p - _b) * (p - _c));
} public override void Display()
{
Console.WriteLine("{0} 三条边长:{1},{2},{3},面积:{3}", _name, _a, _b, _c, this.Area());
}
}
}

  Graphics.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.CompositePattern.Practical
{
public class Graphics : Shape
{
private List<Shape> _children = new List<Shape>(); public Graphics(string name)
: base(name)
{ } public override double Area()
{
double sum = ;
foreach (Shape child in _children)
{
sum += child.Area();
}
return sum;
} public override void Display()
{
foreach (Shape child in _children)
{
child.Display();
} Console.WriteLine("{0} 总面积:{1}", _name, this.Area());
} public void Add(Shape child)
{
_children.Add(child);
} public void Remove(Shape child)
{
_children.Remove(child);
}
}
}

  Client.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.CompositePattern.Practical
{
public class Client
{
static void Main(string[] args)
{
Graphics graphics = new Graphics("全部图形"); Circle circle = new Circle("圆形", );
graphics.Add(circle); Rectangle rectangle = new Rectangle("矩形", , );
graphics.Add(rectangle); Triangle triangle = new Triangle("三角形", , , );
graphics.Add(triangle); graphics.Display();
}
}
}

  运行结果:

圆形 半径:,面积:78.54
矩形 长:,宽:,面积:
三角形 三条边长:,,,面积:
全部图形 总面积:104.54
请按任意键继续. . .

5、组合模式应用分析

  组合模式可以适用以下情形:

  ◊ 希望把对象表示成部分—整体层次结构;

  ◊ 希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中所有对象。

  组合模式具有以下特点:

  ◊ 定义了包含基本对象和组合对象的类层次结构。基本对象可以被组合成更复杂的组合对象,而这个组合对象又可以被组合,不断的递归下去。客户代码中,任何用到基本对象的地方都可以使用组合对象;

  ◊ 简化客户代码。客户可以一致地使用组合结构和单个对象。这样用户就不必关心处理的是一个叶子节点还是一个组合组件,从而简化了客户代码;

  ◊ 使得新增类型的组件更加容易。新定义的Composite或Leaf子类自动地与已有的结构和客户代码一起协同工作,客户程序不需因新的Component类而改变。

6、参考资料

http://www.dofactory.com/Patterns/PatternComposite.aspx

C#设计模式系列:组合模式(Composite)的更多相关文章

  1. 乐在其中设计模式(C#) - 组合模式(Composite Pattern)

    原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...

  2. 二十四种设计模式:组合模式(Composite Pattern)

    组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...

  3. 【设计模式】组合模式 Composite Pattern

    树形结构是软件行业很常见的一种结构,几乎随处可见,  比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...

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

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

  5. python 设计模式之组合模式Composite Pattern

    #引入一 文件夹对我们来说很熟悉,文件夹里面可以包含文件夹,也可以包含文件. 那么文件夹是个容器,文件夹里面的文件夹也是个容器,文件夹里面的文件是对象. 这是一个树形结构 咱们生活工作中常用的一种结构 ...

  6. 设计模式-12组合模式(Composite Pattern)

    1.模式动机 很多时候会存在"部分-整体"的关系,例如:大学中的部门与学院.总公司中的部门与分公司.学习用品中的书与书包.在软件开发中也是这样,例如,文件系统中的文件与文件夹.窗体 ...

  7. Android设计模式系列-组合模式

    Android中对组合模式的应用,可谓是泛滥成粥,随处可见,那就是View和ViewGroup类的使用.在android UI设计,几乎所有的widget和布局类都依靠这两个类.组合模式,Compos ...

  8. 设计模式 笔记 组合模式 Composite

    //---------------------------15/04/16---------------------------- //Composite 组合模式----对象结构型模式 /* 1:意 ...

  9. 【设计模式】—— 组合模式Composite

    前言:[模式总览]——————————by xingoo 模式意图 使对象组合成树形的结构.使用户对单个对象和组合对象的使用具有一致性. 应用场景 1 表示对象的 部分-整体 层次结构 2 忽略组合对 ...

  10. 结构型设计模式之组合模式(Composite)

    结构 意图 将对象组合成树形结构以表示“部分-整体”的层次结构.C o m p o s i t e 使得用户对单个对象和组合对象的使用具有一致性. 适用性 你想表示对象的部分-整体层次结构. 你希望用 ...

随机推荐

  1. Codeforces 486E LIS of Sequence 题解

    题目大意: 一个序列,问其中每一个元素是否为所有最长上升子序列中的元素或是几个但不是所有最长上升子序列中的元素或一个最长上升子序列都不是. 思路: 求以每一个元素为开头和结尾的最长上升子序列长度,若两 ...

  2. rake deploy ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to解决方法

    需要修改项目中Rakefile文件的内容: 原始内容:system "git push origin #{deploy_branch}" 改后内容:system "git ...

  3. 弱省互测#1 t3

    题意 给出一棵n个点的树,求包含1号点的第k小的连通块权值和.(\(n<=10^5\)) 分析 k小一般考虑堆... 题解 堆中关键字为\(s(x)+min(a)\),其中\(s(x)\)表示\ ...

  4. Hibernate Session中的save(),update(),delete(),saveOrUpdate() 细粒度分析

    Hibernate在对资料库进行操作之前,必须先取得Session实例,相当于JDBC在对资料库操作之前,必须先取得Connection实例, Session是Hibernate操作的基础,它不是设计 ...

  5. 响应式web网站设计制作方法

    在研究响应式的时候,记录了一些感想,分享出来,抛砖引玉,希望可以和大家一起讨论.总结下来,响应式比之前想象的要复杂得多.1. ie9以下(不包括ie9)采用ie条件注释,为ie8以及一下单独开一个样式 ...

  6. java记录

    1. 包装类与自动装箱问题:在justjavac的博客上看到翻译的一篇文章 离开java,寻找更佳语言的十大理由 中关于自动装箱的一个描述: 这个特性是为了解决因原生类型的存在所导致的问题,在Java ...

  7. eclipse导入项目出现叹号处理方法:

    1.选中该项目名称,单击右键 2.点击Properties 3.选中Java Build Path 4. 5. 6. 7.出现红叉的解决办法 8. 9. 10. 11. 12. 按照以上步骤操作就可以 ...

  8. 使用Python中PIL图形库进行截屏

    目的:通过使用Python的一个图形库PIL(Python Image Library)对屏幕进行截图 步骤: 1.下载PIL(路径)并安装 2.新建文件“截屏.py”,右键Edit with IDL ...

  9. JavaScript编码规范

    1 代码风格 1.1 结构语句 [强制] 不得省略语句结束的分号. [强制] 在 if / else / for / do / while 语句中,即使只有一行,也不得省略块 {...}. 示例: / ...

  10. RSA密钥生成与使用

    RSA密钥生成与使用 openssl生成工具链接:http://pan.baidu.com/s/1c0v3UxE 密码:uv48 1. 打开openssl密钥生成软件打开 openssl 文件夹下的  ...