六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 877|回复: 0

php设计模式 Composite (组合模式)

[复制链接]
 楼主| 发表于 2013-8-1 15:55:28 | 显示全部楼层 |阅读模式
  1. <?php
  2. /**
  3. * 组合模式
  4. *
  5. * 将对象组合成树形结构以表示"部分-整体"的层次结构,使得客户对单个对象和复合对象的使用具有一致性
  6. */
  7. abstractclass MenuComponent
  8. {
  9. publicfunction add($component){}
  10. publicfunction remove($component){}
  11. publicfunction getName(){}
  12. publicfunction getUrl(){}
  13. publicfunction display(){}
  14. }

  15. class Menu extends MenuComponent
  16. {
  17. private$_items=array();
  18. private$_name=null;

  19. publicfunction __construct($name)
  20.     {
  21. $this->_name =$name;
  22.     }

  23. publicfunction add($component)
  24.     {
  25. $this->_items[] =$component;
  26.     }

  27. publicfunction remove($component)
  28.     {
  29. $key=array_search($component,$this->_items);
  30. if($key!==false) unset($this->_items[$key]);
  31.     }

  32. publicfunction display()
  33.     {
  34. echo"-- ".$this->_name." ---------
  35. ";
  36. foreach($this->_items as$item)
  37.         {
  38. $item->display();
  39.         }
  40.     }
  41. }

  42. class Item extends MenuComponent
  43. {
  44. private$_name=null;
  45. private$_url=null;

  46. publicfunction __construct($name,$url)
  47.     {
  48. $this->_name =$name;
  49. $this->_url =$url;
  50.     }

  51. publicfunction display()
  52.     {
  53. echo$this->_name."#".$this->_url."
  54. ";
  55.     }
  56. }

  57. class Client
  58. {
  59. private$_menu=null;

  60. publicfunction __construct($menu)
  61.     {
  62. $this->_menu =$menu;
  63.     }

  64. publicfunction setMenu($menu)
  65.     {
  66. $this->_menu =$menu;
  67.     }

  68. publicfunction displayMenu()
  69.     {
  70. $this->_menu->display();
  71.     }
  72. }

  73. // 实例一下
  74. // 创建menu
  75. $subMenu1=new Menu("sub menu1");
  76. $subMenu2=new Menu("sub menu2");
  77. $subMenu3=new Menu("sub menu3");

  78. $item1=new Item("163","www.163.com");
  79. $item2=new Item("sina","www.sina.com");

  80. $subMenu1->add($item1);
  81. $subMenu1->add($item2);

  82. $item3=new Item("baidu","www.baidu.com");
  83. $item4=new Item("google","www.google.com");
  84. $subMenu2->add($item3);
  85. $subMenu2->add($item4);

  86. $allMenu=new Menu("All Menu");
  87. $allMenu->add($subMenu1);
  88. $allMenu->add($subMenu2);
  89. $allMenu->add($subMenu3);

  90. $objClient=new Client($allMenu);
  91. $objClient->displayMenu();

  92. $objClient->setMenu($subMenu2);
  93. $objClient->displayMenu();
复制代码
本文摘自:http://www.cnblogs.com/bluefrog/archive/2011/06/20/2085536.html

代码包下载:
(传统的23种模式(没有区分简单工厂与抽象工厂)
http://it.agoit.com/thread-419151-1-1.html  php设计模式 Interpreter(解释器模式)
http://it.agoit.com/thread-419152-1-1.html  php设计模式 Factory(工厂模式)
http://it.agoit.com/thread-419153-1-1.html  php设计模式 Facade(外观模式)
http://it.agoit.com/thread-419154-1-1.html  php设计模式 Decorator(装饰模式)
http://it.agoit.com/thread-419155-1-1.html  php设计模式 Builder(建造者模式)
http://it.agoit.com/thread-419156-1-1.html  php设计模式 Adapter(适配器模式)
http://it.agoit.com/thread-419157-1-1.html  php设计模式 Template (模板模式)
http://it.agoit.com/thread-419158-1-1.html  php设计模式 Command(命令模式)
http://it.agoit.com/thread-419159-1-1.html  php设计模式 Singleton(单例模式)
http://it.agoit.com/thread-419160-1-1.html  php设计模式 Observer(观察者模式)
http://it.agoit.com/thread-419161-1-1.html  php设计模式 Strategy(策略模式)
http://it.agoit.com/thread-419162-1-1.html  php设计模式 Visitor (访问者模式)
http://it.agoit.com/thread-419163-1-1.html  php设计模式 Memento (备忘录模式)
http://it.agoit.com/thread-419164-1-1.html php设计模式 Prototype (原型模式)
http://it.agoit.com/thread-419165-1-1.html php设计模式 Mediator (中介者模式)
http://it.agoit.com/thread-419166-1-1.html php设计模式 FlyWeight (享元模式)
http://it.agoit.com/thread-419167-1-1.html php设计模式 Chain Of Responsibility (职责链模式)
http://it.agoit.com/thread-419168-1-1.html php设计模式 Bridge (桥接模式)
http://it.agoit.com/thread-419169-1-1.html php设计模式 Proxy (代理模式)
http://it.agoit.com/thread-419170-1-1.html php设计模式 State (状态模式)
http://it.agoit.com/thread-419171-1-1.html php设计模式 Composite (组合模式)
http://it.agoit.com/thread-419172-1-1.html php设计模式 Interator (迭代器模式)
下面来自<<php设计模式>>
http://it.agoit.com/thread-419173-1-1.html php设计模式 DAO(数据访问对象模式)
http://it.agoit.com/thread-419174-1-1.html php设计模式 Delegation(委托模式)


该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表