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

八、安全式的合成模式PHP示例
<?php
/**
* 抽象組件角色
*/
interface Component {
/**
* 返回自己的實(shí)例
*/
public function getComposite();
/**
* 示例方法
*/
public function operation();
}
/**
* 樹枝組件角色
*/
class Composite implements Component {
private $_composites;
public function __construct() {
$this->_composites = array();
}
public function getComposite() {
return $this;
}
/**
* 示例方法,調(diào)用各個(gè)子對象的operation方法
*/
public function operation() {
echo 'Composite operation begin:<br />';
foreach ($this->_composites as $composite) {
$composite->operation();
}
echo 'Composite operation end:<br /><br />';
}
/**
* 聚集管理方法 添加一個(gè)子對象
* @param Component $component 子對象
*/
public function add(Component $component) {
$this->_composites[] = $component;
}
/**
* 聚集管理方法 刪除一個(gè)子對象
* @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實(shí)現(xiàn)合成模式的代碼,還有一些關(guān)于合成模式的概念區(qū)分,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
有關(guān)于PHP中常見數(shù)據(jù)類型的匯總分享
本文整理了有關(guān)于PHP中常見的數(shù)據(jù)類型,感興趣的朋友可以參考下2014-01-01
PHP strtok()函數(shù)的優(yōu)點(diǎn)分析
相對于explode()來說,strtok()函數(shù)可以控制節(jié)奏。按需切割字串。2010-03-03
php自動(dòng)給文章加關(guān)鍵詞鏈接的函數(shù)代碼
自動(dòng)給文章加關(guān)鍵詞鏈接的php函數(shù)代碼,需要的朋友可以參考下2012-11-11
學(xué)習(xí)php過程中的一些注意點(diǎn)的總結(jié)
在學(xué)習(xí)php的過程中會有一些細(xì)節(jié)是需要注意的,本文整理了一些比較實(shí)際的問題,希望對大家有所幫助2013-10-10
Admin generator, filters and I18n
You need to modify your EntityFormFilter (where Entity is your object class - Article, Book, etc.).2011-10-10
php字符串替換函數(shù)substr_replace()用法實(shí)例
這篇文章主要介紹了php字符串替換函數(shù)substr_replace()用法,實(shí)例分析了php中substr_replace函數(shù)的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
與文件上傳有關(guān)的php配置參數(shù)總結(jié)
搞個(gè)了圖片上傳,死活不好使,后來發(fā)現(xiàn)是php參數(shù)配置的問題,下面總結(jié)下與文件上傳有關(guān)的php參數(shù),有類似情況的朋友可以參考下哈2013-06-06
php結(jié)合md5實(shí)現(xiàn)的加密解密方法
這篇文章主要介紹了php結(jié)合md5實(shí)現(xiàn)的加密解密方法,涉及PHP字符串操作及加密解密算法實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01

