學習php設計模式 php實現合成模式(composite)_php技巧

來源:互聯網
上載者:User

一、意圖
將對象組合成樹形結構以表示”部分-整體”的階層。Composite使使用者對單個對象和組合對象的使用具有一致性。
Composite變化的是一個對象的結構和組成。
二、合成模式中主要角色
抽象組件(Component)角色:抽象角色,給參加組合的對象規定一個介面。在適當的情況下,實現所有類共有介面的預設行為。聲明一個介面用於訪問和管理Component的子組件
樹葉組件(Leaf)角色:在組合中表示分葉節點對象,分葉節點沒有子節點。在組合中定義圖元對象的行為。
樹枝組件(Composite)角色:儲存子組件。定義有子組件的那些組件的行為。在Component介面中實現與子組件有關的操作。
用戶端(Client):通過Component介面操縱組合組件的對象
三、合成模式的優點和缺點
Composite模式的優點
1、簡化客戶代碼
2、使得更容易增加新類型的組件

Composite模式的缺點:使你的設計變得更加一般化,容易增加組件也會產生一些問題,那就是很難限制組合中的組件
四、合成模式適用情境
1、你想表示對象的部分-整體階層
2、你希望使用者忽略組合對象和單個對象的不同,使用者將統一地使用組合結構中的所有對象。
五、合成模式與其它模式
裝飾器模式:Decorator模式經常與Composite模式一起使用。當裝飾與合成一起使用時,它們通常有一個公用的父類。因此裝飾必須支援具有add,remove和getChild操作的Component介面
享元模式:Flyweight模式讓你共用組件,但不再引用他們的父組件
迭代器模式:Itertor可用來遍曆Composite
訪問者模式:Visitor將本來應該分布在Composite和Leaf類中的操作和行為局部化。
六、安全式的合成模式
在Composite類裡面聲明所有的用來管理子類對象的方法。這樣的做法是安全的。因為樹葉類型的對象根本就沒有管理子類的方法,因此,如果用戶端對樹葉類對象使用這些方法時,程式會在編譯時間期出錯。編譯通不過,就不會出現運行時期錯誤
這樣的缺點是不夠透明,因為樹葉類和合成類將具有不同的介面。
七、安全式的合成模式結構圖 

八、安全式的合成模式PHP樣本

<?php/** * 抽象組件角色 */interface Component {  /**  * 返回自己的執行個體  */ public function getComposite();  /**  * 樣本方法  */ public function operation();} /** * 樹枝組件角色 */class Composite implements Component { private $_composites;  public function __construct() {  $this->_composites = array(); }  public function getComposite() {  return $this; }  /**  * 樣本方法,調用各個子物件的operation方法  */ public function operation() {  echo 'Composite operation begin:<br />';  foreach ($this->_composites as $composite) {   $composite->operation();  }  echo 'Composite operation end:<br /><br />'; }  /**  * 聚集管理方法 添加一個子物件  * @param Component $component 子物件  */ public function add(Component $component) {  $this->_composites[] = $component; }  /**  * 聚集管理方法 刪除一個子物件  * @param Component $component 子物件  * @return boolean 刪除是否成功  */ public function remove(Component $component) {  foreach ($this->_composites as $key => $row) {   if ($component == $row) {    unset($this->_composites[$key]);    return TRUE;   }  }   return FALSE; }  /**  * 聚集管理方法 返回所有的子物件  */ 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; }}  /** * 用戶端 */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實現合成模式的代碼,還有一些關於合成模式的概念區分,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.