惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
Last Week in AI
Last Week in AI
月光博客
月光博客
D
DataBreaches.Net
WordPress大学
WordPress大学
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
C
Check Point Blog
F
Fortinet All Blogs
B
Blog
小众软件
小众软件
Vercel News
Vercel News
罗磊的独立博客
有赞技术团队
有赞技术团队

博客园 - Tristan(GuoZhijian)

Core Design Patterns(16) Chain of Responsibility 职责链模式 Core Design Patterns(15) Template Method 模版方法模式 Core Design Patterns(14) State 状态模式 Core Design Patterns(13) Strategy 策略模式 Core Design Patterns(12) Builder 建造者模式 Core Design Patterns(11) Abstract Factory 抽象工厂模式 Core Design Patterns(10) Singleton 单例模式 Core Design Patterns(9) Factory Method 工厂方法模式 Core Design Patterns(8) Prototype 原型模式 Core Design Patterns(7) Facade 外观模式 Core Design Patterns(6) Adapter 适配器模式 Core Design Patterns(5) Flyweight 享元模式 Core Design Patterns(3) Bridge 桥接模式 Core Design Patterns(2) Proxy 代理模式 Core Design Patterns(1) Decorator 装饰模式 老调重弹:插件式框架开发的一个简单应用 Behavior模型应用:可拖动的div容器 Microsoft Asp.Net Ajax框架入门(13) PageRequestManager Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
Core Design Patterns(4) Composite 组合模式
Tristan(GuoZhijian) · 2008-03-07 · via 博客园 - Tristan(GuoZhijian)

VS 2008

组合模式使得我们可以以同样的方式看待单一Comonent和Component集合。
也就是,单一对象和该对象的组合具有相同的抽象行为。
组合模式通常用于构建树型结构。

1. 模式UML图
    
    

2. 应用
    
    项目中有一组部门信息,部门之间具有父子关系,是一个典型的树型结构,如有一个部门叫市政局,下属有市管处、公路处两个部门,公路处下属又有公路署部门等等。

Department.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Composite.BLL {
    
public class Department {

        
public int DepartmentId getset; }
        
public string DepartmentName getset; }
    }

}

IDepartment.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Composite.BLL {
    
public interface IDepartment {

        
void Add(IDepartment department);
        
void Remove(IDepartment department);
        
int GetChildCount();
    }

}

DepartmentLeaf.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Composite.BLL {
    
public class DepartmentLeaf : IDepartment {

        
private Department department;

        
public Department Department {
            
get return this.department; }
        }


        
public DepartmentLeaf(Department department) {
            
this.department = department;
        }


        
IDepartment Members
    }

}

DepartmentComposite.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Composite.BLL {
    
public class DepartmentComposite : IDepartment {

        
private Department department;
        
private List<IDepartment> list = new List<IDepartment>();

        
public Department Department {
            
get return this.department; }
        }


        
public DepartmentComposite(Department department) {
            
this.department = department;
        }

    
        
IDepartment Members
   }

}


Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.Composite.BLL;

namespace DesignPattern.Composite {
    
class Program {
        
static void Main(string[] args) {


            IDepartment j1 
= new DepartmentComposite(new Department() { DepartmentId=1, DepartmentName="市政局" });

            IDepartment c1 
= new DepartmentComposite(new Department() { DepartmentId = 2, DepartmentName = "市管处" });

            IDepartment c2 
= new DepartmentComposite(new Department() { DepartmentId = 3, DepartmentName = "公路处" });

            IDepartment s1 
= new DepartmentLeaf(new Department() { DepartmentId = 4, DepartmentName = "公路署" });

            c2.Add(s1);
            j1.Add(c1);
            j1.Add(c2);

            Console.WriteLine(j1.GetChildCount().ToString());
            Console.WriteLine(c1.GetChildCount().ToString());
            Console.WriteLine(c2.GetChildCount().ToString());
            Console.WriteLine(s1.GetChildCount().ToString());

        }

    }

}


Output

3. 思考
    
    对于组合模式,通常可以将前述的UML进行退还,我们甚至可以不要Leaf类,为了灵活扩展性,每个叶子都可以演变为树枝。
    对于本例的部门树型结构其实只是选择了一个最简单的行为进行实现,真正的树,需要实现的行为还远不止这些。