码迷,mamicode.com
首页 > 其他好文 > 详细

组合模式

时间:2014-12-17 17:51:20      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   os   使用   sp   

组合模式:

将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得用户对单个对象和组合对象的使用具有一致性。

在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面,例如多级树形菜单,文件和文件夹目录

 

思维导图:

bubuko.com,布布扣

构件模式的组成:

抽象构件角色(component):是组合中的对象声明接口,在适当的情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理Component子部件。

树叶构件角色(Leaf):在组合树中表示叶节点对象,叶节点没有子节点。并在组合中定义图元对象的行为。

树枝构件角色(Composite):定义有子部件的那些部件的行为。存储子部件。在Component接口中实现与子部件有关的操作。

客户角色(Client):通过component接口操纵组合部件的对象。

 

代码:

 

<?php  
 
/**
 * 抽象构件角色(Component)
 *
 */ 
abstract class MenuComponent 
{ 
    public function add($component){} 
    public function remove($component){} 
    public function getName(){} 
    public function getUrl(){} 
    public function displayOperation(){} 
} 
/**
 * 树枝构件角色(Composite)
 *
 */ 
class MenuComposite extends MenuComponent 
{ 
    private $_items = array(); 
    private $_name = null; 
     
    public function __construct($name) { 
        $this->_name = $name; 
    } 
    public function add($component) { 
        $this->_items[$component->getName()] = $component; 
    } 
    public function remove($component) { 
        $key = array_search($component,$this->_items); 
        if($key !== false) unset($this->_items[$key]); 
    } 
    public function getItems() { 
        return $this->_items; 
    } 
       
    public function displayOperation($prefix=‘|‘) { 
 
        if($this->getItems()) { 
            $prefix .= ‘ _ _ ‘; 
        }else{ 
            $prefix .=‘‘; 
        } 
        echo $this->_name, " <br />\n";
        foreach($this->_items as $name=> $item) { 
            echo $prefix; 
            $item->displayOperation($prefix); 
        } 
    } 
   
    public function getName(){ 
        return $this->_name; 
    } 
} 
   
/**
 *树叶构件角色(Leaf)
 *
 */ 
class ItemLeaf extends MenuComponent 
{ 
    private $_name = null; 
    private $_url = null; 
 
    public function __construct($name,$url) 
    { 
        $this->_name = $name; 
        $this->_url = $url; 
    } 
   
    public function displayOperation($prefix=‘‘) 
    { 
        echo ‘<a href="‘, $this->_url, ‘">‘ , $this->_name, "</a><br />\n";
    } 
   
    public function getName(){ 
        return $this->_name; 
    } 
} 
   
 
 
 
$bj = new MenuComposite("北京");
 
$cy = new ItemLeaf("朝阳区","chaoyang.com");
$hd = new ItemLeaf("海淀区","haidian.com");
   
$bj->add($cy);
$bj->add($hd);
 
$yn = new MenuComposite("云南");
$zt = new MenuComposite("昭通市");
$sj = new ItemLeaf("绥江县", "suijiang.gov.cn");
 
$zt->add($sj);
$yn->add($zt);
 
$sd = new MenuComposite("山东"); 
$sd->add(new ItemLeaf("济南", "jinan.gov.cn"));
 
   
$allMenu = new MenuComposite("中国"); 
$allMenu->add($bj); 
$allMenu->add($yn); 
$allMenu->add($sd);
 
 
$allMenu->displayOperation(); 
 
 
// 只显示云南的
$yn->displayOperation();

 

 

 

总结:

组合模式解耦了客户程序与复杂元素内部结构,从而使客户程序可以向处理简单元素一样来处理复杂元素。如果你想要创建层次结构,并可以在其中以相同的方式对待所有元素,那么组合模式就是最理想的选择。

 

组合模式

标签:style   blog   http   ar   io   color   os   使用   sp   

原文地址:http://www.cnblogs.com/leezhxing/p/4169813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!