Core Design Patterns(4) Composite 组合模式
2008-03-07 22:21
288 查看
VS 2008
组合模式使得我们可以以同样的方式看待单一Comonent和Component集合。
也就是,单一对象和该对象的组合具有相同的抽象行为。
组合模式通常用于构建树型结构。
1. 模式UML图
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
DepartmentLeaf.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.Composite.BLL;
namespace DesignPattern.Composite {
{
{
{ DepartmentId=1, DepartmentName="市政局" });
{ DepartmentId = 2, DepartmentName = "市管处" });
{ DepartmentId = 3, DepartmentName = "公路处" });
{ 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
![](http://images.cnblogs.com/cnblogs_com/guozhijian/Design Pattern/com-1.gif)
3. 思考
对于组合模式,通常可以将前述的UML进行退还,我们甚至可以不要Leaf类,为了灵活扩展性,每个叶子都可以演变为树枝。
对于本例的部门树型结构其实只是选择了一个最简单的行为进行实现,真正的树,需要实现的行为还远不止这些。
组合模式使得我们可以以同样的方式看待单一Comonent和Component集合。
也就是,单一对象和该对象的组合具有相同的抽象行为。
组合模式通常用于构建树型结构。
1. 模式UML图
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
DepartmentLeaf.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.Composite.BLL;
namespace DesignPattern.Composite {
{
{
{ DepartmentId=1, DepartmentName="市政局" });
{ DepartmentId = 2, DepartmentName = "市管处" });
{ DepartmentId = 3, DepartmentName = "公路处" });
{ 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
![](http://images.cnblogs.com/cnblogs_com/guozhijian/Design Pattern/com-1.gif)
3. 思考
对于组合模式,通常可以将前述的UML进行退还,我们甚至可以不要Leaf类,为了灵活扩展性,每个叶子都可以演变为树枝。
对于本例的部门树型结构其实只是选择了一个最简单的行为进行实现,真正的树,需要实现的行为还远不止这些。
相关文章推荐
- 组合模式——Head First Design Patterns
- 设计模式之----组合模式(Composite Design Pattern)
- Composite Design Pattern 组合设计模式
- 《Head First设计模式》 读书笔记10 迭代器与组合模式 The Iterator and Composite Patterns
- Core Design Patterns(1) Decorator 装饰模式
- Core Design Patterns(2) Proxy 代理模式
- 《Head First Design Patterns》笔记十一:组合模式(Composite Pattern)
- Core Design Patterns(12) Builder 建造者模式
- 设计模式学习—组合实体模式(Composite Entity Design Pattern)
- Core Design Patterns(13) Strategy 策略模式
- Core Design Patterns(7) Facade 外观模式
- Core Design Patterns(8) Prototype 原型模式
- Core Design Patterns(15) Template Method 模版方法模式
- Core Design Patterns(16) Chain of Responsibility 职责链模式
- Core Design Patterns(10) Singleton 单例模式
- Core Design Patterns(14) State 状态模式
- Core Design Patterns(3) Bridge 桥接模式
- Composite Design Pattern 设计模式组合
- 程序设计模式(八) C++ 组合(Composite)模式
- 组合模式(Composite)