Net设计模式实例之组合模式(Composite Pattern)(1)

简介:

一、组合模式简介(Brief Introduction

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

二、解决的问题(What To Solve

解决整合与部分可以被一致对待问题。

三、组合模式分析(Analysis

1、组合模式结构

Component 类:组合中的对象声明接口,在适当情况下,实现所有类共有接口的行为。声明一个接口用于访问和管理 Component 的子部件
Leaf 类:叶节点对象,叶节点没有子节点。由于叶节点不能增加分支和树叶,所以叶节点的 Add Remove 没有实际意义。
有叶节点行为,用来存储叶节点集合
Composite 类:实现 Componet 的相关操作,比如 Add Remove 操作。
children: 用来存储叶节点集合

2、源代码

1 、抽象类 Component
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  Diaplay(int  depth);
}
 
2 、叶子节点 Leaf  继承于 Component
public  class Leaf :Component
{
 
    public  Leaf(string  name)
         :base (name)
     {
       
     }
 
    public override void  Add(Component  c)
     {
        Console .WriteLine(" 不能向叶子节点添加子节点" );
     }
 
    public override void  Remove(Component  c)
     {
        Console .WriteLine(" 叶子节点没有子节点" );
     }
 
    public override void  Diaplay(int  depth)
     {
        Console .WriteLine(new string ('-' ,depth)+name);
     }
}
 
3 、组合类 Composite 继承于 Component 拥有枝节点行为
public  class Composite  : Component
{
 
    List <Component > children;
 
    public  Composite(string  name)
         :base (name)
     {
        if  (children == null )
         {
             children = new List <Component >();
         }
     }
 
    public override void  Add(Component  c)
     {
        this .children.Add(c);
     }
 
    public override void  Remove(Component  c)
     {
        this .children.Remove(c);
     }
 
    public override void  Diaplay(int  depth)
     {
        Console .WriteLine(new String ('-' ,depth)+name);
        foreach  (Component  component in  children)
         {
             component.Diaplay(depth + 2);
         }
     }
}
 
 
4 、客户端代码
static  void Main (string [] args)
{
    Composite  root = new Composite (" 根节点root" );
     root.Add(new Leaf (" 根上生出的叶子A" ));
     root.Add(new Leaf (" 根上生出的叶子B" ));
 
    Composite  comp = new Composite (" 根上生出的分支CompositeX" );
     comp.Add(new Leaf (" 分支CompositeX 生出的叶子LeafXA" ));
     comp.Add(new Leaf (" 分支CompositeX 生出的叶子LeafXB" ));
 
     root.Add(comp);
 
    Composite  comp2 = new Composite (" 分支CompositeX 生出的分支CompositeXY" );
     comp2.Add(new Leaf (" 分支CompositeXY 生出叶子LeafXYA" ));
     comp2.Add(new Leaf (" 分支CompositeXY 生出叶子LeafXYB" ));
 
     comp.Add(comp2);
 
     root.Add(new Leaf (" 根节点生成的叶子LeafC" ));
    Leaf  leafD = new Leaf ("leaf D" );
     root.Add(leafD);
     root.Remove(leafD);
     root.Diaplay(1);
    Console .Read();
}
 

3、程序运行结果











本文转自 灵动生活 51CTO博客,原文链接:http://blog.51cto.com/smartlife/267502,如需转载请自行联系原作者

目录
相关文章
|
5月前
|
设计模式
二十三种设计模式全面解析-解密组合模式(Composite Pattern):构建统一而强大的对象结构
二十三种设计模式全面解析-解密组合模式(Composite Pattern):构建统一而强大的对象结构
|
5月前
|
设计模式 存储 Java
认真学习设计模式之组合模式(Composite Pattern)
认真学习设计模式之组合模式(Composite Pattern)
25 0
|
7月前
|
设计模式 Java uml
设计模式15 - 组合模式【Composite Pattern】
设计模式15 - 组合模式【Composite Pattern】
19 0
|
7月前
|
设计模式 Java 容器
【设计模式——学习笔记】23种设计模式——组合模式Composite(原理讲解+应用场景介绍+案例介绍+Java代码实现)
【设计模式——学习笔记】23种设计模式——组合模式Composite(原理讲解+应用场景介绍+案例介绍+Java代码实现)
55 0
【设计模式——学习笔记】23种设计模式——组合模式Composite(原理讲解+应用场景介绍+案例介绍+Java代码实现)
|
7月前
|
设计模式 Java 容器
设计模式~组合模式(composite)-16
目录 (1)优点: (2)缺点: (3)使用场景: (4)注意事项: (5)应用实例 代码
30 0
|
12月前
|
设计模式 Java 数据安全/隐私保护
Java设计模式-组合模式(Composite)
Java设计模式-组合模式(Composite)
|
设计模式 存储 容器
从零开始学设计模式(十一):组合模式(Composite Pattern):
组合模式(Composite Pattern)又叫做部分-整体模式,它在树型结构(可以想象一下数据结构中的树)的问题中,模糊了简单元素和复杂元素的概念,客户端程序可以像处理简单元素一样来处理复杂元素,而使得客户端程序与复杂元素的内部结构进行解藕。
121 0
从零开始学设计模式(十一):组合模式(Composite Pattern):
|
设计模式 存储 容器
【愚公系列】2021年12月 二十三种设计模式(八)-组合模式(Composite Pattern)
【愚公系列】2021年12月 二十三种设计模式(八)-组合模式(Composite Pattern)
【愚公系列】2021年12月 二十三种设计模式(八)-组合模式(Composite Pattern)
|
存储 设计模式 Java
浅谈JAVA设计模式之——组合模式(Composite)
将对象组合成树形结构以表示"部分-整体"的层次结构。"Composite使得用户对单个对象和组合对象的使用具有一致性。
198 0
浅谈JAVA设计模式之——组合模式(Composite)
|
前端开发 .NET C#
ASP.NET Core MVC 设计模式 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core MVC 设计模式 - ASP.NET Core 基础教程 - 简单教程,简单编程   ASP.NET Core MVC 设计模式 上一章节中,我们提到 ASP.NET Core 支持 MVC 开发模式,不知道大家对 MVC 设计模式是否了解,算了,不管了,本章节我们就来讲讲 MVC 设计模式吧。
2334 0