设计模式のCompositePattern(组合模式)----结构模式
一、产生背景
又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。
这种模式创建了一个包含自己对象组的类。该类提供了修改相同对象组的方式。
我们通过下面的实例来演示组合模式的用法。实例演示了一个组织中员工的层次结构。
二、通常做法
组合模式参与者:
◊ Component
° 声明组合中对象的接口;
° 实现全部类中公共接口的默认行为;
° 声明访问和管理子类的接口;
° (可选择)定义接口提供在递归结构中访问父类。
◊ Leaf
° 表示在组合对象中叶子节点对象,没有子节点;
° 定义组合对象中的初始行为。
◊ Composite
° 定义Component子类的行为;
° 保存Component子类;
° 实现Component接口的子类关联操作。
◊ Client
° 通过Component接口组合多个对象。
三、实例
1、component.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CompositionPattern
{
public abstract class Component
{
protected string _name; public Component(string name)
{
_name = name;
} public abstract void Add(Component c); public abstract void Remove(Component c); public abstract void Display(int depth); }
}
2、composite.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CompositionPattern
{
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 + );
}
}
}
}
3、leaf.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CompositionPattern
{
public class Leaf:Component
{
public Leaf(string name) : base(name) { } public override void Add(Component c)
{
Console.WriteLine("Cannot add to a");
} 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);
}
}
}
4、客户端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CompositionPattern
{
class Program
{
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(); Console.ReadLine(); }
}
}
四、设计模式分析
组合模式可以适用以下情形:
◊ 希望把对象表示成部分—整体层次结构;
◊ 希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中所有对象。
组合模式具有以下特点:
◊ 定义了包含基本对象和组合对象的类层次结构。基本对象可以被组合成更复杂的组合对象,而这个组合对象又可以被组合,不断的递归下去。客户代码中,任何用到基本对象的地方都可以使用组合对象;
◊ 简化客户代码。客户可以一致地使用组合结构和单个对象。这样用户就不必关心处理的是一个叶子节点还是一个组合组件,从而简化了客户代码;
◊ 使得新增类型的组件更加容易。新定义的Composite或Leaf子类自动地与已有的结构和客户代码一起协同工作,客户程序不需因新的Component类而改变。
设计模式のCompositePattern(组合模式)----结构模式的更多相关文章
- 十二、享元(Flyweight)模式--结构模式(Structural Pattern)
Flyweight在拳击比赛中指最轻量级,即"蝇量级",有些作者翻译为"羽量级".这里使用"享元 模式"更能反映模式的用意. 享元模式以共享 ...
- 九、 合成(Composite)模式 --结构模式(Structural Pattern)
合成模式:有时又叫做部分-整体模式(Part-Whole).合成模式将对象组织到树结构中,可以用来描述整体与部分的关系.合成模式可以使客户端将单纯元素与复合元素同等看待. 合成模式分为安全式和透明式 ...
- 十、装饰(Decorator)模式 --结构模式(Structural Pattern)
装饰(Decorator)模式又名包装(Wrapper)模式[GOF95].装饰模式以对客户端透明的方 式扩展对象的功能,是继承关系的一个替代方案. 装饰模式类图: 类图说明: 抽象构件(Compon ...
- 七、适配器(Adapter)模式--结构模式(Structural Pattern)
适配器模式:把一个类的接口变换成客户端所期待的另一种接口,从而使原本接口不匹配而无法在一起工作的两个类能够在一起工作. 类的 Adapter模式的结构: 类适配器类图: 由图中可以看出,Adaptee ...
- 十一、外观(Facade)模式--结构模式(Structural Pattern)
外部与一个子系统的通信必须通过一个统一的门面(Facade)对象进行,这就是门面模式.门面模式要求一个子系统的外部与其内部的通信必须通过一个统一的门面(Facade)对象进行. 门面模式提供一个高层次 ...
- 八、桥接模式--结构模式(Structural Pattern)
桥梁模式:将抽象化(Abstraction)与实现化 (Implementation)脱耦,使得二者可以独立地变化. 桥梁模式类图: 抽象化(Abstraction)角色:抽象化给出的定义,并保存 一 ...
- C#设计模式(10)——组合模式(Composite Pattern)
一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...
- 设计模式:组合模式(Composite)
定 义:将对象组合树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象使用具有一致性. 结构图: Component类: abstract class Component ...
- [JAVA设计模式]第三部分:结构模式
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
随机推荐
- spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法
spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版 ...
- 【转】AtomicReference与volatile的区别
来源:AtomicReference与volatile的区别 AtomicReference与volatile的在可见性上的意义是一致的. volatile不能保证原子性,AutomicReferen ...
- Http协议请求头、响应头、响应码
Http部分请求头 Accept 客户机通过这个头,告诉服务器,它支持哪些数据类型 Accept-Charset 客户机通过这个头,告诉服务器,它支持的编码 Accept-Encoding 客户机通过 ...
- 你所不知道的JSON.stringify
译者按: 老司机们,你知道JSON.stringify还有第二个和第三个可选参数吗?它们是什么呢? 原文: What you didn’t know about JSON.Stringify 译者: ...
- CSS border-collapse 属性
表格边框合并: table { border-collapse:collapse; 边框会合并为一个单一的边框 } ----------------------------------------- ...
- 洛谷P5205 【模板】多项式开根(多项式sqrt)
题意 题目链接 Sol 这个就很没意思了 求个ln,然后系数除以2,然后exp回去. #include<bits/stdc++.h> #define Pair pair<int, i ...
- 洛谷P4170 [CQOI2007]涂色(区间dp)
题意 题目链接 Sol 震惊,某知名竞赛网站竟照搬省选原题! 裸的区间dp,\(f[l][r]\)表示干掉\([l, r]\)的最小花费,昨天写的时候比较困于是就把能想到的转移都写了.. // luo ...
- Python 'xxx' codec can't decode byte xxx常见编码错
'xxx' codec can't decode byte xxx常见编码错误处理 by:授客 QQ:1033553122 测试环境 python 3.3.2 win7 问题描述 利用python文件 ...
- android adb 流程原理代码分析(一)
由于要用到adb的知识,但是对adb啥也不了解,看了下android的代码,adb的源码在system/core/adb下面,然后网上搜下了资料,发现很多大神的源码分析,瞬间信心爆棚,把大神写的博客都 ...
- python leetcode 字符串相乘
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...