php设计模式 Composite (组合模式)
<?php/**
* 组合模式
*
* 将对象组合成树形结构以表示"部分-整体"的层次结构,使得客户对单个对象和复合对象的使用具有一致性
*/
abstractclass MenuComponent
{
publicfunction add($component){}
publicfunction remove($component){}
publicfunction getName(){}
publicfunction getUrl(){}
publicfunction display(){}
}
class Menu extends MenuComponent
{
private$_items=array();
private$_name=null;
publicfunction __construct($name)
{
$this->_name =$name;
}
publicfunction add($component)
{
$this->_items[] =$component;
}
publicfunction remove($component)
{
$key=array_search($component,$this->_items);
if($key!==false) unset($this->_items[$key]);
}
publicfunction display()
{
echo"-- ".$this->_name." ---------
";
foreach($this->_items as$item)
{
$item->display();
}
}
}
class Item extends MenuComponent
{
private$_name=null;
private$_url=null;
publicfunction __construct($name,$url)
{
$this->_name =$name;
$this->_url =$url;
}
publicfunction display()
{
echo$this->_name."#".$this->_url."
";
}
}
class Client
{
private$_menu=null;
publicfunction __construct($menu)
{
$this->_menu =$menu;
}
publicfunction setMenu($menu)
{
$this->_menu =$menu;
}
publicfunction displayMenu()
{
$this->_menu->display();
}
}
// 实例一下
// 创建menu
$subMenu1=new Menu("sub menu1");
$subMenu2=new Menu("sub menu2");
$subMenu3=new Menu("sub menu3");
$item1=new Item("163","www.163.com");
$item2=new Item("sina","www.sina.com");
$subMenu1->add($item1);
$subMenu1->add($item2);
$item3=new Item("baidu","www.baidu.com");
$item4=new Item("google","www.google.com");
$subMenu2->add($item3);
$subMenu2->add($item4);
$allMenu=new Menu("All Menu");
$allMenu->add($subMenu1);
$allMenu->add($subMenu2);
$allMenu->add($subMenu3);
$objClient=new Client($allMenu);
$objClient->displayMenu();
$objClient->setMenu($subMenu2);
$objClient->displayMenu();本文摘自:http://www.cnblogs.com/bluefrog/archive/2011/06/20/2085536.html
代码包下载:
(传统的23种模式(没有区分简单工厂与抽象工厂)
http://it.agoit.com/thread-419151-1-1.htmlphp设计模式 Interpreter(解释器模式)
http://it.agoit.com/thread-419152-1-1.htmlphp设计模式 Factory(工厂模式)
http://it.agoit.com/thread-419153-1-1.htmlphp设计模式 Facade(外观模式)
http://it.agoit.com/thread-419154-1-1.htmlphp设计模式 Decorator(装饰模式)
http://it.agoit.com/thread-419155-1-1.htmlphp设计模式 Builder(建造者模式)
http://it.agoit.com/thread-419156-1-1.htmlphp设计模式 Adapter(适配器模式)
http://it.agoit.com/thread-419157-1-1.htmlphp设计模式 Template (模板模式)
http://it.agoit.com/thread-419158-1-1.htmlphp设计模式 Command(命令模式)
http://it.agoit.com/thread-419159-1-1.htmlphp设计模式 Singleton(单例模式)
http://it.agoit.com/thread-419160-1-1.htmlphp设计模式 Observer(观察者模式)
http://it.agoit.com/thread-419161-1-1.htmlphp设计模式 Strategy(策略模式)
http://it.agoit.com/thread-419162-1-1.htmlphp设计模式 Visitor (访问者模式)
http://it.agoit.com/thread-419163-1-1.htmlphp设计模式 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(委托模式)
页:
[1]