Design Pattern - Structural Patterns - Composite Pattern

50 篇文章 0 订阅
37 篇文章 0 订阅

2007

Section 3, Chapter 4


Composite Pattern


Concept

The Composite pattern allows you to compound different subsets of functionality into a collection and then call each subset in turn in the collection at a given point.


Use

You had a collection of objects, and each object had a particular method that you wanted to call in series.


Design

A composite allows you to call the classes with a single method and loop through the collection, calling the method on each class.


  • Component - The base class with the common functionality for all classes.
  • Leaf - the object that makes up the individual objects that exist within the collection.
  • Composite - the class that forms the collection object itself.


Both the leaf and the composite classes inherit from the component, so that whether you call the individual leaf classes or the composite class, you still call the same method(s).




Illustration




//Component
abstract class Component
{
	private string _name;
	public Component(string name)
	{
		_name = name;
	}
	public string Name
	{
		get{return _name;}
	}
	public abstract void Draw();
	public override int GetHashCode()
	{
		return _name.GetHashCode();
	}
	public override bool Equals(object obj)
	{
		if (obj != null && obj is Component)
			return _name.Equals(((Component)obj).Name);
		else
			return false;
	}
	public override string ToString()
	{
		return _name;
	}
}

//Leaf
class GraphicalComponent : Component
{
	public GraphicalComponent(string name) : base(name){}
	public override void Draw()
	{
		......
	}
}

//composite
class GraphicalComposite : Component
{
	private ArrayList _children = new ArrayList();

	public GraphicalComposite(string name) : base(name){}

	public void Add(Component child)
	{
		_children.Add(child);
	}

	public void Remove(Component child)
	{
		_children.Remove(child);
	}

	public Component GetChild(int index)
	{
		return (Component)_children[index];
	}

	public override void Draw()
	{
		foreach(Component comp in _children)
			comp.Draw();
	}
}


GraphicalComposite root = new GraphicalComposite("OuterPanel");
root.Add(new GraphicalComponent("Circle"));
root.Add(new GraphicalComponent("Square"));
root.Add(new GraphicalComponent("Triangle"));

GraphicalComposite childComposite = new GraphicalComposite("InnerPanel");
childComposite.Add(new GraphicalComponent("Line"));

root.Add(childComposite);


GraphicalComponent removable = new GraphicalComponent("Single Line");
root.Add(removable);
root.Remove(removable);


A composite class can also contain other composite classes.

You can have a two-sided relationship, which means the leaf objects know about their parent composite object.







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值