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

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
IT之家
IT之家
C
Check Point Blog
T
The Blog of Author Tim Ferriss
S
SegmentFault 最新的问题
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
M
MIT News - Artificial intelligence
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
F
Fortinet All Blogs
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
爱范儿
爱范儿
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)

博客园 - 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类,为了灵活扩展性,每个叶子都可以演变为树枝。
    对于本例的部门树型结构其实只是选择了一个最简单的行为进行实现,真正的树,需要实现的行为还远不止这些。