[review]Design Pattern:Composite

Composite

Composite pattern is dedicated to resolve the whole and part issue in application. The whole can be parted into different parts and the parts can be composited into a very whole, to resolve the whole-part issue.

When we use this pattern

if you want to express the whole-part hierarchy circumstance, like the TreeView in ASP.NET WinForm. like the dom in Javascript, the part has the same structure but it maybe belongs to another part(another part's child) . the part also can have the children with the same stucture as itself. 

Every part here we call it as a component. obviously, it is better for users that they just consider everything as a component no matter how the very component is composited, no matter how many children or leaves the component has. Just a componeng from the user's Perspective

Roles in this pattern

  • Component: define the interfaces for the component. implement the default functions and any other function for managing the component .
  • Leaf: the very component but with no children. in ther real world. it is like a leaf
  • Composite: the very component getting all functions of the component. designed for storing the children
  • Client: communicate with the component.
This pattern is easy to understand but hard to say(at least for me). thinking carefully about the TreeView in C# or the real tree can help you get a better understanding for this pattern.
 
A small demo designed for this:
namespace Composite
{
public abstract class Component
{
public abstract void Operation();

public abstract void Add(Component com);

public abstract void Remove(Component com);

}

public class Comosite : Component
{
List<Component> children = new List<Component>();
public override void Operation()
{
//
}

public override void Add(Component com)
{
children.Add(com);
}

public override void Remove(Component com)
{
if (children.Count > 0)
{
children.Remove(com);
}
}
}

public class Leaf : Component
{
public override void Operation()
{
//
}

public override void Add(Component com)
{
throw new InvalidOperationException();//can't implement the add function on leaf
}

public override void Remove(Component com)
{
throw new InvalidOperationException();//can't implement the remove function on leaf
}
}

class Client
{
static void Main(string[] args)
{
Component root = new Comosite();

root.Add(new Leaf());
root.Add(new Leaf());

Component children1 = new Comosite();

Component children2 = new Comosite();

root.Add(children1);
root.Add(children2);

}
}
}
This situation is very often in our real world, ranges from the treeview to the a company's structure hierachy. IMO, to better understand this pattern you got to think more about the real world.

转载于:https://www.cnblogs.com/sanjia/archive/2011/11/08/2241788.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值