• 第十九章 分公司 = 一部分 --- 组合模式

    第十九章 分公司 = 一部分 --- 组合模式

    1. <?php
    2. // component为组合中的对象接口,在适当的情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理Component的字部件。
    3. abstract class Component
    4. {
    5. protected $name;
    6. function __construct($name)
    7. {
    8. $this->name = $name;
    9. }
    10. //通常用add和remove方法来提供增加或移除树枝货树叶的功能
    11. abstract public function add(Component $c);
    12. abstract public function remove(Component $c);
    13. abstract public function display($depth);
    14. }
    15. //leaf在组合中表示叶节点对象,叶节点对象没有子节点。
    16. class Leaf extends Component
    17. {
    18. // 由于叶子没有再增加分枝和树叶,所以add和remove方法实现它没有意义,
    19. // 但这样做可以消除叶节点和枝节点对象在抽象层次的区别,它们具有完全一致的接口
    20. public function add(Component $c)
    21. {
    22. echo "can not add to a leaf\n";
    23. }
    24. public function remove(Component $c)
    25. {
    26. echo "can not remove to a leaf\n";
    27. }
    28. // 叶节点的具体方法,此处是显示其名称和级别
    29. public function display($depth)
    30. {
    31. echo str_repeat('-', $depth).$this->name."\n";
    32. }
    33. }
    34. //composite定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关的操作,比如增加add和删除remove.
    35. class Composite extends Component
    36. {
    37. //一个子对象集合用来存储其下属的枝节点和叶节点。
    38. private $childern = [];
    39. public function add(Component $c)
    40. {
    41. array_push($this->childern, $c);
    42. }
    43. public function remove(Component $c)
    44. {
    45. foreach ($this->childern as $key => $value) {
    46. if ($c === $value) {
    47. unset($this->childern[$key]);
    48. }
    49. }
    50. }
    51. // 显示其枝节点名称,并对其下级进行遍历
    52. public function display($depth)
    53. {
    54. echo str_repeat('-', $depth).$this->name."\n";
    55. foreach ($this->childern as $component) {
    56. $component->display($depth + 2);
    57. }
    58. }
    59. }
    60. //客户端代码
    61. $root = new Composite('root');
    62. $root->add(new Leaf("Leaf A"));
    63. $root->add(new Leaf("Leaf B"));
    64. $comp = new Composite("Composite X");
    65. $comp->add(new Leaf("Leaf XA"));
    66. $comp->add(new Leaf("Leaf XB"));
    67. $root->add($comp);
    68. $comp2 = new Composite("Composite X");
    69. $comp2->add(new Leaf("Leaf XA"));
    70. $comp2->add(new Leaf("Leaf XB"));
    71. $comp->add($comp2);
    72. $root->add(new Leaf("Leaf C"));
    73. $leaf = new Leaf("Leaf D");
    74. $root->add($leaf);
    75. $root->remove($leaf);
    76. $root->display(1);

    总结:

    组合模式,将对象组合成树形结构以表示‘部分与整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

    透明方式,子类的所有接口一致,虽然有些接口没有用。

    安全方式,子类接口不一致,只实现特定的接口,但是这样就要做相应的判断,带来了不便。

    需求中是体现部分与整体层次的结构时,或希望用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象时,就应该考虑用组合模式了。

    组合模式可以让客户一致地使用组合结构和单个对象。

    上一章:第十八章 如果再回到从前 --- 备忘录模式

    下一章:第二十章 想走?可以!先买票 --- 迭代器模式