极速理解设计模式系列: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)

 

 
  1. //部件抽象接口角色:Component 
  2. abstract class ControlComponent 
  3.     public string ControlName { get; set; } 
  4.     public double Width { get; set; } 
  5.     public double Height { get; set; } 
  6.     public int AllowInputNum { get; set; } 
  7.     abstract public void AddNode(ControlComponent con); 
  8.     abstract public void RemoveNode(ControlComponent con); 
  9.     abstract public void ShowNodeInfo(); 

        二、叶角色(Leaf)

 

 
  1. //叶角色:Leaf 
  2.  class ControlLeaf : ControlComponent 
  3.  { 
  4.      //叶角色没有子节点,故不能添加删除子节点 
  5.      public override void AddNode(ControlComponent con) 
  6.      { 
  7.          Console.WriteLine("叶类无法添加子节点。"); 
  8.      } 
  9.  
  10.      public override void RemoveNode(ControlComponent con) 
  11.      { 
  12.          Console.WriteLine("叶类无法移出子节点。"); 
  13.      } 
  14.  
  15.      //显示子节点信息的方法 
  16.      public override void ShowNodeInfo() 
  17.      { 
  18.          Console.WriteLine("___控件名:" + ControlName + " 控件宽度:" + Width + " 控件高度:" + Height + " 控件允许输入:" + AllowInputNum); 
  19.      } 
  20.  } 

        三、组合类角色(Composite)

 

 
  1. //组合类:Composite(主干类) 
  2.  class ControlComposite : ControlComponent 
  3.  { 
  4.      //子节点的容器(可以是叶节点,叶可以是下一节主干) 
  5.      private List<ControlComponent> controlList = new List<ControlComponent>(); 
  6.  
  7.      //添加和删除子节点 
  8.      public override void AddNode(ControlComponent con) 
  9.      { 
  10.          controlList.Add(con); 
  11.      } 
  12.  
  13.      public override void RemoveNode(ControlComponent con) 
  14.      { 
  15.          controlList.Remove(con); 
  16.      } 
  17.  
  18.      //显示子节点 
  19.      public override void ShowNodeInfo() 
  20.      { 
  21.          Console.WriteLine("控件名:" + ControlName + " 控件宽度:" + Width + " 控件高度:" + Height + " 控件允许输入:" + AllowInputNum); 
  22.          //递归显示其子节点树 
  23.          foreach (ControlComponent c in controlList) 
  24.          { 
  25.              c.ShowNodeInfo(); 
  26.          } 
  27.      } 
  28.  } 

        四、客户端角色(Client)

 

 
  1. //客户端:Client 
  2. class Program 
  3.     static void Main(string[] args) 
  4.     { 
  5.         ControlComposite controlLayout = new ControlComposite() { ControlName = "Layout", Height = 300, Width = 300 }; 
  6.         controlLayout.AddNode(new ControlLeaf() { ControlName = "___TextBox", Height = 30, Width = 180, AllowInputNum = 50 }); 
  7.         controlLayout.AddNode(new ControlLeaf() { ControlName = "___RichTextBox", Height = 35, Width = 220, AllowInputNum = 1000 }); 
  8.         controlLayout.AddNode(new ControlLeaf() { ControlName = "___RadioButton", Height = 25, Width = 300, AllowInputNum = 1 }); 
  9.  
  10.         ControlComposite complexControl = new ControlComposite() { ControlName = "complex", Height = 96, Width = 300 }; 
  11.         complexControl.AddNode(new ControlLeaf() { ControlName = "___RadioButtonA", Height = 33, Width = 180, AllowInputNum = 50 }); 
  12.         complexControl.AddNode(new ControlLeaf() { ControlName = "___ComboBoxB", Height = 33, Width = 100 }); 
  13.         complexControl.AddNode(new ControlLeaf() { ControlName = "___TextBoxC", Height = 30, Width = 100 }); 
  14.  
  15.         controlLayout.AddNode(complexControl); 
  16.         // controlLayout.RemoveNode(complexControl); 
  17.  
  18.         controlLayout.ShowNodeInfo(); 
  19.  
  20.         Console.ReadLine(); 
  21.     } 

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



本文转自程兴亮 51CTO博客,原文链接:http://blog.51cto.com/chengxingliang/826952

相关文章
|
5月前
|
设计模式 存储 Java
认真学习设计模式之组合模式(Composite Pattern)
认真学习设计模式之组合模式(Composite Pattern)
24 0
|
7月前
|
设计模式 Java uml
设计模式15 - 组合模式【Composite Pattern】
设计模式15 - 组合模式【Composite Pattern】
19 0
|
7月前
|
设计模式 Java 容器
设计模式~组合模式(composite)-16
目录 (1)优点: (2)缺点: (3)使用场景: (4)注意事项: (5)应用实例 代码
30 0
|
存储 设计模式 安全
结构型模式 - 组合模式(Composite Pattern)
结构型模式 - 组合模式(Composite Pattern)
|
设计模式 存储 容器
【愚公系列】2021年12月 二十三种设计模式(八)-组合模式(Composite Pattern)
【愚公系列】2021年12月 二十三种设计模式(八)-组合模式(Composite Pattern)
【愚公系列】2021年12月 二十三种设计模式(八)-组合模式(Composite Pattern)
|
安全 C# 容器
C#设计模式之九组合模式(Composite Pattern)【结构型】
原文:C#设计模式之九组合模式(Composite Pattern)【结构型】 一、引言      今天我们要讲【结构型】设计模式的第四个模式,该模式是【组合模式】,英文名称是:Composite Pattern。
1093 0