极速理解设计模式系列:5.组合模式(Composite Pattern)

四个角色:部件抽象接口角色(Component)、叶角色(Leaf)、组合类角色(Composite)、客户端角色(Client)

        部件抽象接口角色(Component):定义组合类对象的公共行为、属性和管理子部件的方法接口。

        叶角色(Leaf):实现Component的公共行为,但是无法管理子部件,为最终叶节点。

        组合类角色(Composite):实现Component的公共行为,可以管理子节点(增、删、查)。

        客户端角色(Client):通过Component控制整棵组合对象树。

         实现思路:一棵树,分为叶节点和主干节点,通过一个统一的接口可以往下添加节点和删除节点。

 类图:

  

        应用场景:Silverlight中一个Canvas名为Layout,需要有很多行,第一行是一个TextBox,第二行是一个RichTextBox,第三行是RadioButton,第四行是一个复杂的组合控件名为complex其内部有RadioButtonA、ComboBoxB、TextBoxC三个子控件。

        分析:这里实际上可以看做是一棵树型结构的容器Layout,内部有4个控件,其中3个简单控件是叶节点,最后一个复杂控件是主干,主干下面还有3个叶片节点。

        下面我们在控制台程序去演示一下如何使用Composite Pattern:

        一、部件抽象接口角色(Component)

    //部件抽象接口角色:Component
abstract class ControlComponent
{
public string ControlName { get; set; }
public double Width { get; set; }
public double Height { get; set; }
public int AllowInputNum { get; set; }
abstract public void AddNode(ControlComponent con);
abstract public void RemoveNode(ControlComponent con);
abstract public void ShowNodeInfo();
}

        二、叶角色(Leaf)

   //叶角色:Leaf
class ControlLeaf : ControlComponent
{
//叶角色没有子节点,故不能添加删除子节点
public override void AddNode(ControlComponent con)
{
Console.WriteLine(
"叶类无法添加子节点。");
}

public override void RemoveNode(ControlComponent con)
{
Console.WriteLine(
"叶类无法移出子节点。");
}

//显示子节点信息的方法
public override void ShowNodeInfo()
{
Console.WriteLine(
"___控件名:" + ControlName + " 控件宽度:" + Width + " 控件高度:" + Height + " 控件允许输入:" + AllowInputNum);
}
}

        三、组合类角色(Composite)

   //组合类:Composite(主干类)
class ControlComposite : ControlComponent
{
//子节点的容器(可以是叶节点,叶可以是下一节主干)
private List<ControlComponent> controlList = new List<ControlComponent>();

//添加和删除子节点
public override void AddNode(ControlComponent con)
{
controlList.Add(con);
}

public override void RemoveNode(ControlComponent con)
{
controlList.Remove(con);
}

//显示子节点
public override void ShowNodeInfo()
{
Console.WriteLine(
"控件名:" + ControlName + " 控件宽度:" + Width + " 控件高度:" + Height + " 控件允许输入:" + AllowInputNum);
//递归显示其子节点树
foreach (ControlComponent c in controlList)
{
c.ShowNodeInfo();
}
}
}

        四、客户端角色(Client)

    //客户端:Client
class Program
{
static void Main(string[] args)
{
ControlComposite controlLayout
= new ControlComposite() { ControlName = "Layout", Height = 300, Width = 300 };
controlLayout.AddNode(
new ControlLeaf() { ControlName = "___TextBox", Height = 30, Width = 180, AllowInputNum = 50 });
controlLayout.AddNode(
new ControlLeaf() { ControlName = "___RichTextBox", Height = 35, Width = 220, AllowInputNum = 1000 });
controlLayout.AddNode(
new ControlLeaf() { ControlName = "___RadioButton", Height = 25, Width = 300, AllowInputNum = 1 });

ControlComposite complexControl
= new ControlComposite() { ControlName = "complex", Height = 96, Width = 300 };
complexControl.AddNode(
new ControlLeaf() { ControlName = "___RadioButtonA", Height = 33, Width = 180, AllowInputNum = 50 });
complexControl.AddNode(
new ControlLeaf() { ControlName = "___ComboBoxB", Height = 33, Width = 100 });
complexControl.AddNode(
new ControlLeaf() { ControlName = "___TextBoxC", Height = 30, Width = 100 });

controlLayout.AddNode(complexControl);
// controlLayout.RemoveNode(complexControl);

controlLayout.ShowNodeInfo();

Console.ReadLine();
}
}

        如需源码请点击 CompositePattern.rar 下载。

posted @ 2011-09-02 10:15  .NET架构  阅读(2560)  评论(4编辑  收藏  举报