经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
设计模式-结构型-组合模式
来源:cnblogs  作者:酷学大叔  时间:2019/9/29 9:03:26  对本文有异议

组合模式(Composite):

定义:

  组合模式又叫部分整体模式,它是一种将对象组合成树状的层次结构模式,用来表示"部分-整体"的关系,使用户对单个对象和组合对象具有一致的访问性。

组合模式的角色:

  1)抽象构建(Component):它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成。

  2)树叶构件(Leaf):是组合中的叶节点对象,它没有子节点,用于实现抽象构件角色中 声明的公共接口。

  3)树枝构件(Composite):是组合中的分支节点对象,它有子节点。它实现了抽象构件角色中声明的接口,它的主要作用是存储和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法。

  

  1. 1 internal class Program
  2. 2 {
  3. 3 private static void Main(string[] args)
  4. 4 {
  5. 5 // Create a tree structure
  6. 6 Composite root = new Composite("root");
  7. 7 root.Add(new Leaf("Leaf A"));
  8. 8 root.Add(new Leaf("Leaf B"));
  9. 9
  10. 10 Composite comp = new Composite("Composite X");
  11. 11 comp.Add(new Leaf("Leaf XA"));
  12. 12 comp.Add(new Leaf("Leaf XB"));
  13. 13
  14. 14 root.Add(comp);
  15. 15 root.Add(new Leaf("Leaf C"));
  16. 16
  17. 17 // Add and remove a leaf
  18. 18 Leaf leaf = new Leaf("Leaf D");
  19. 19 root.Add(leaf);
  20. 20 root.Remove(leaf);
  21. 21
  22. 22 // Recursively display tree
  23. 23 root.Display(1);
  24. 24 }
  25. 25 }
  26. 26
  27. 27 public abstract class Component
  28. 28 {
  29. 29 protected string _name;
  30. 30
  31. 31 public Component(string name)
  32. 32 {
  33. 33 this._name = name;
  34. 34 }
  35. 35
  36. 36 public abstract void Add(Component c);
  37. 37
  38. 38 public abstract void Remove(Component c);
  39. 39
  40. 40 public abstract void Display(int depth);
  41. 41 }
  42. 42
  43. 43 public class Leaf : Component
  44. 44 {
  45. 45 public Leaf(string name)
  46. 46 : base(name)
  47. 47 {
  48. 48 }
  49. 49
  50. 50 public override void Add(Component c)
  51. 51 {
  52. 52 Console.WriteLine("Cannot add to a leaf");
  53. 53 }
  54. 54
  55. 55 public override void Remove(Component c)
  56. 56 {
  57. 57 Console.WriteLine("Cannot remove from a leaf");
  58. 58 }
  59. 59
  60. 60 public override void Display(int depth)
  61. 61 {
  62. 62 Console.WriteLine(new String('-', depth) + _name);
  63. 63 }
  64. 64 }
  65. 65
  66. 66 public class Composite : Component
  67. 67 {
  68. 68 private List<Component> _children = new List<Component>();
  69. 69
  70. 70 public Composite(string name)
  71. 71 : base(name)
  72. 72 {
  73. 73 }
  74. 74
  75. 75 public override void Add(Component component)
  76. 76 {
  77. 77 _children.Add(component);
  78. 78 }
  79. 79
  80. 80 public override void Remove(Component component)
  81. 81 {
  82. 82 _children.Remove(component);
  83. 83 }
  84. 84
  85. 85 public override void Display(int depth)
  86. 86 {
  87. 87 Console.WriteLine(new String('-', depth) + _name);
  88. 88
  89. 89 foreach (Component component in _children)
  90. 90 {
  91. 91 component.Display(depth + 2);
  92. 92 }
  93. 93 }
  94. 94 }

 极简版如下,将显示部分可以拿到客户端进行:

  1. 1 internal class Program
  2. 2 {
  3. 3 private static void Main(string[] args)
  4. 4 {
  5. 5 // Create a tree structure
  6. 6 Component root = new Component("root");
  7. 7 root.Add(new Component("Leaf A"));
  8. 8 root.Add(new Component("Leaf B"));
  9. 9
  10. 10 Component comp = new Component("Composite X");
  11. 11 comp.Add(new Component("Leaf XA"));
  12. 12 comp.Add(new Component("Leaf XB"));
  13. 13
  14. 14 root.Add(comp);
  15. 15 root.Add(new Component("Leaf C"));
  16. 16
  17. 17 // Add and remove a leaf
  18. 18 Component leaf = new Component("Leaf D");
  19. 19 root.Add(leaf);
  20. 20 root.Remove(leaf);
  21. 21
  22. 22 // 由客户端显示,Component只进行组合
  23. 23 }
  24. 24 }
  25. 25
  26. 26 public class Component
  27. 27 {
  28. 28 protected string _name;
  29. 29 private List<Component> _children = new List<Component>();
  30. 30
  31. 31 public Component(string name)
  32. 32 {
  33. 33 this._name = name;
  34. 34 }
  35. 35
  36. 36 public void Add(Component c)
  37. 37 {
  38. 38 _children.Add(c);
  39. 39 }
  40. 40
  41. 41 public void Remove(Component c)
  42. 42 {
  43. 43 _children.Remove(c);
  44. 44 }
  45. 45
  46. 46 public List<Component> GetChild()
  47. 47 {
  48. 48 return _children;
  49. 49 }
  50. 50 }

是不是恍然大悟,就是我们在做权限管理的时候用到的一对多的关系。

组合模式的优缺点:

  优点:

    1)组合模式使得客户端代码可以一致地处理单个对象和组合对象,无须关心自己处理的单个对象还是组合对象,这简化了客户端代码;

    2)更容易在组合体内加入新的对象,客户端不会因为加入新的对象而更改源代码,满足OCP原则。

  缺点:

    1)设计较复杂,客户端需要花更多的时间理清类之间的层次关系;

    2)不容易限制容器中的构件;

    3)不容易用继承的方法来增加构件的新功能。

参考:https://www.cnblogs.com/libingql/p/3496345.html

原文链接:http://www.cnblogs.com/az4215/p/11602936.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号