当前位置:Gxlcms > PHP教程 > PHP组合模式第二中实现方法

PHP组合模式第二中实现方法

时间:2021-07-01 10:21:17 帮助过:28人阅读

PHP组合模式第二中实现方法

  1. <?php
  2. // 组合模式
  3. interface Component
  4. {
  5. public function doAction();
  6. public function addComponent(Component $c);
  7. public function removeComponent(Component $c);
  8. }
  9. abstract class AbstractComponent
  10. {
  11. // public abstract function doAction();
  12. /**
  13. * default implementation
  14. */
  15. public function addComponent(Component $c)
  16. {
  17. throw new Exception('This is leaf node, can\'t be call addComponent method!');
  18. }
  19. /**
  20. * default implementation
  21. */
  22. public function removeComponent(Component $c)
  23. {
  24. throw new Exception('This is leaf node, can\'t be call removeComponent method!');
  25. }
  26. }
  27. /**
  28. * 局部类
  29. * 可能并不需要 addComponent, removeComponent 方法,但是必须实现
  30. * 可以用抽象类作为超类(AbstractComponent),需要实现的覆盖即可,不需要实现的若调用则会发出异常。
  31. */
  32. class Leaf implements Component
  33. {
  34. public function doAction()
  35. {
  36. echoLine('The [leaf] doAction!');
  37. }
  38. /**
  39. * null implementation
  40. */
  41. public function addComponent(Component $c) { }
  42. /**
  43. * null implementation
  44. */
  45. public function removeComponent(Component $c) { }
  46. }
  47. /**
  48. * 组合模式的一个问题是如何实现 add 和 remove 方法。一般的组合模式会在抽象超类中添加 add
  49. * 和 remove 方法。这可以确保模式中的所有类都共享同一个借口,但这同时也意味着局部类也必须
  50. * 实现这些方法。实际上,我们并不希望在局部类中实现这些方法。
  51. *
  52. * 需要担心的问题:
  53. * 1. 组合操作的成本。
  54. * 2. 对象持久化问题。难以直接将数据保存在关系型数据中,但其数据却非常适合持久化为 XML。
  55. */
  56. class Composite implements Component
  57. {
  58. /**
  59. * component container
  60. */
  61. private $container = array();
  62. public function doAction()
  63. {
  64. echoLine('The [Composite] doAction!');
  65. foreach ($this->container as $c)
  66. $c->doAction();
  67. }
  68. /**
  69. * add component
  70. * @param Component $c
  71. */
  72. public function addComponent(Component $c)
  73. {
  74. if(!in_array($c, $this->container, true))
  75. $this->container[] = $c;
  76. }
  77. /**
  78. * remove conponent
  79. * @param Component $c
  80. */
  81. public function removeComponent(Component $c)
  82. {
  83. $this->container = array_diff($this->container, array($c));
  84. }
  85. /**
  86. * get all components
  87. * @return array
  88. */
  89. public function getComponents()
  90. {
  91. return $this->container;
  92. }
  93. }
  94. // test code
  95. $leaf = new Leaf();
  96. $composite = new Composite();
  97. $composite->addComponent($leaf);
  98. $composite->addComponent(new Leaf());
  99. $composite->addComponent(new Leaf());
  100. $composite->doAction();
  101. /**
  102. * 总结:
  103. * 如果你想像对待单个对象一样对待组合对象,那么组合模式十分有用。可能是因为组合对象本质上和局部对象
  104. * 相似,也可能因为在环境中组合对象和局部对象的特征相同。因为组合是树型结构,所以对整体的操作会影响
  105. * 到局部,而通过组合,对客户端代码来说局部的数据又是透明的。组合模式使这些操作和查询对客户端代码透明。
  106. * 对象树可以方便的进行遍历。你也可以轻松的在组合结构中加入新的组件类型。
  107. * 但另一方面,组合模式又依赖于其组成部分的简单性。随着我们引入复杂的规则,代码会变得越来越难以维护。
  108. * 组合模式不能很好的在关系型数据库中保存数据,但却非常适合使用 XML 持久化。
  109. */

以上就是PHP组合模式第二中实现方法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行