设计模式笔记(十四) —— 组合模式

组合模式(Composite):将对象组合成树形结构以表示“部分——整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
透明方式:也就是说在Component中声明所有用来管理子对象的方法,其中包括Add、Remove等。这样实现Component接口的所有子类都具备了Add和Remove。这样做的好处就是叶节点和枝节点对于外界没有区别,它们具备完全一致的接口。但是问题也很明显,因为Leaf类本身不具备Add()、Remove()方法的功能,所以实现它是没有意义的。
安全方式:也就是在Component接口中不去声明Add和Remove方法,那么子类的Leaf也就不需要去实现它,而是在Composite声明所有用来管理子类对象的方法,不过由于不够透明,所以树叶和树枝类将不具有相同的接口,客户端的调用需要做相应的判断,带来了不便。 

  1. using System;
  2. using System.Collections.Generic;
  3. /**
  4.  *组合模式(Composite):将对象组合成树形结构以表示“部分——整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
  5. 透明方式:也就是说在Component中声明所有用来管理子对象的方法,其中包括Add、Remove等。这样实现Component接口的所有子类都具备了Add和Remove。这样做的好处就是叶节点和枝节点对于外界没有区别,它们具备完全一致的接口。但是问题也很明显,因为Leaf类本身不具备Add()、Remove()方法的功能,所以实现它是没有意义的。
  6. 安全方式:也就是在Component接口中不去声明Add和Remove方法,那么子类的Leaf也就不需要去实现它,而是在Composite声明所有用来管理子类对象的方法,不过由于不够透明,所以树叶和树枝类将不具有相同的接口,客户端的调用需要做相应的判断,带来了不便。
  7.  */
  8. namespace StuDesignMode.Composite
  9. {
  10.     #region 组合模式
  11.     abstract class AbsComponent
  12.     {
  13.         protected string Name { getset; }
  14.         public AbsComponent(string name)
  15.         {
  16.             this.Name = name;
  17.         }
  18.         public abstract void Add(AbsComponent c);
  19.         public abstract void Remove(AbsComponent c);
  20.         public abstract void Display(int depth);
  21.     }
  22.     class Leaf : AbsComponent
  23.     {
  24.         public Leaf(string name) : base(name) { }
  25.         public override void Add(AbsComponent c)
  26.         {
  27.             Console.WriteLine("Cannot add to a leaf!");
  28.         }
  29.         public override void Remove(AbsComponent c)
  30.         {
  31.             Console.WriteLine("Cannot remove to a leaf!");
  32.         }
  33.         public override void Display(int depth)
  34.         {
  35.             Console.WriteLine(new String('—',depth)+this.Name);
  36.         }
  37.     }
  38.     class Composite : AbsComponent
  39.     {
  40.         private List<AbsComponent> childdren = new List<AbsComponent>();
  41.         private Composite(string name):base(name){}
  42.         public override void Add(AbsComponent c)
  43.         {
  44.             childdren.Add(c);
  45.         }
  46.         public override void Remove(AbsComponent c)
  47.         {
  48.             childdren.Remove(c);
  49.         }
  50.         public override void Display(int depth)
  51.         {
  52.             Console.WriteLine(new String('—',depth)+this.Name);   
  53.             foreach (var item in childdren)
  54.             {
  55.                 item.Display(depth + 2);
  56.             }
  57.         }
  58.         class ClientTest
  59.         {
  60.             static void Main(string[] args)
  61.             {
  62.                 Composite root = new Composite("root");
  63.                 root.Add(new Leaf("A"));
  64.                 root.Add(new Leaf("B"));
  65.                 Composite comp = new Composite("comp");
  66.                 comp.Add(new Leaf("x"));
  67.                 comp.Add(new Leaf("y"));
  68.                 root.Add(comp);
  69.                 root.Display(1);
  70.             }
  71.         }
  72.     }
  73.     #endregion
  74. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值