欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)合成模式(composite)

 更新時(shí)間:2015年12月08日 11:06:58   作者:胖胖  
這篇文章主要介紹了php設(shè)計(jì)模式中的合成模式,使用php實(shí)現(xiàn)合成模式,感興趣的小伙伴們可以參考一下

一、意圖
將對(duì)象組合成樹(shù)形結(jié)構(gòu)以表示”部分-整體”的層次結(jié)構(gòu)。Composite使用戶(hù)對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致性。
Composite變化的是一個(gè)對(duì)象的結(jié)構(gòu)和組成。
二、合成模式中主要角色
抽象組件(Component)角色:抽象角色,給參加組合的對(duì)象規(guī)定一個(gè)接口。在適當(dāng)?shù)那闆r下,實(shí)現(xiàn)所有類(lèi)共有接口的缺省行為。聲明一個(gè)接口用于訪問(wèn)和管理Component的子組件
樹(shù)葉組件(Leaf)角色:在組合中表示葉節(jié)點(diǎn)對(duì)象,葉節(jié)點(diǎn)沒(méi)有子節(jié)點(diǎn)。在組合中定義圖元對(duì)象的行為。
樹(shù)枝組件(Composite)角色:存儲(chǔ)子部件。定義有子部件的那些部件的行為。在Component接口中實(shí)現(xiàn)與子部件有關(guān)的操作。
客戶(hù)端(Client):通過(guò)Component接口操縱組合部件的對(duì)象
三、合成模式的優(yōu)點(diǎn)和缺點(diǎn)
Composite模式的優(yōu)點(diǎn)
1、簡(jiǎn)化客戶(hù)代碼
2、使得更容易增加新類(lèi)型的組件

Composite模式的缺點(diǎn):使你的設(shè)計(jì)變得更加一般化,容易增加組件也會(huì)產(chǎn)生一些問(wèn)題,那就是很難限制組合中的組件
四、合成模式適用場(chǎng)景
1、你想表示對(duì)象的部分-整體層次結(jié)構(gòu)
2、你希望用戶(hù)忽略組合對(duì)象和單個(gè)對(duì)象的不同,用戶(hù)將統(tǒng)一地使用組合結(jié)構(gòu)中的所有對(duì)象。
五、合成模式與其它模式
裝飾器模式:Decorator模式經(jīng)常與Composite模式一起使用。當(dāng)裝飾與合成一起使用時(shí),它們通常有一個(gè)公共的父類(lèi)。因此裝飾必須支持具有add,remove和getChild操作的Component接口
享元模式:Flyweight模式讓你共享組件,但不再引用他們的父部件
迭代器模式:Itertor可用來(lái)遍歷Composite
訪問(wèn)者模式:Visitor將本來(lái)應(yīng)該分布在Composite和Leaf類(lèi)中的操作和行為局部化。
六、安全式的合成模式
在Composite類(lèi)里面聲明所有的用來(lái)管理子類(lèi)對(duì)象的方法。這樣的做法是安全的。因?yàn)闃?shù)葉類(lèi)型的對(duì)象根本就沒(méi)有管理子類(lèi)的方法,因此,如果客戶(hù)端對(duì)樹(shù)葉類(lèi)對(duì)象使用這些方法時(shí),程序會(huì)在編譯時(shí)期出錯(cuò)。編譯通不過(guò),就不會(huì)出現(xiàn)運(yùn)行時(shí)期錯(cuò)誤
這樣的缺點(diǎn)是不夠透明,因?yàn)闃?shù)葉類(lèi)和合成類(lèi)將具有不同的接口。
七、安全式的合成模式結(jié)構(gòu)圖 

八、安全式的合成模式PHP示例

<?php
/**
 * 抽象組件角色
 */
interface Component {
 
 /**
  * 返回自己的實(shí)例
  */
 public function getComposite();
 
 /**
  * 示例方法
  */
 public function operation();
}
 
/**
 * 樹(shù)枝組件角色
 */
class Composite implements Component {
 private $_composites;
 
 public function __construct() {
  $this->_composites = array();
 }
 
 public function getComposite() {
  return $this;
 }
 
 /**
  * 示例方法,調(diào)用各個(gè)子對(duì)象的operation方法
  */
 public function operation() {
  echo 'Composite operation begin:<br />';
  foreach ($this->_composites as $composite) {
   $composite->operation();
  }
  echo 'Composite operation end:<br /><br />';
 }
 
 /**
  * 聚集管理方法 添加一個(gè)子對(duì)象
  * @param Component $component 子對(duì)象
  */
 public function add(Component $component) {
  $this->_composites[] = $component;
 }
 
 /**
  * 聚集管理方法 刪除一個(gè)子對(duì)象
  * @param Component $component 子對(duì)象
  * @return boolean 刪除是否成功
  */
 public function remove(Component $component) {
  foreach ($this->_composites as $key => $row) {
   if ($component == $row) {
    unset($this->_composites[$key]);
    return TRUE;
   }
  }
 
  return FALSE;
 }
 
 /**
  * 聚集管理方法 返回所有的子對(duì)象
  */
 public function getChild() {
  return $this->_composites;
 }
 
}
 
class Leaf implements Component {
 private $_name;
 
 public function __construct($name) {
  $this->_name = $name;
 }
 
 public function operation() {
  echo 'Leaf operation ', $this->_name, '<br />';
 }
 
 public function getComposite() {
  return null;
 }
}
 
 
/**
 * 客戶(hù)端
 */
class Client {
 
 /**
  * Main program.
  */
 public static function main() {
  $leaf1 = new Leaf('first');
  $leaf2 = new Leaf('second');
 
  $composite = new Composite();
  $composite->add($leaf1);
  $composite->add($leaf2);
  $composite->operation();
 
  $composite->remove($leaf2);
  $composite->operation();
 }
 
}
 
Client::main();
?>

以上就是使用php實(shí)現(xiàn)合成模式的代碼,還有一些關(guān)于合成模式的概念區(qū)分,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論