這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在php中應(yīng)用裝飾者模式,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

裝飾模式指的是在不必改變原類文件和使用繼承的情況下,動態(tài)地擴展一個對象的功能。它是通過創(chuàng)建一個包裝對象,也就是裝飾來包裹真實的對象。
示例:
A、B、C編輯同一篇文章。
class Article{
protected $content;
public function __construct($info){
$this->content = $info;
}
}
class editor_A extends Article{
public function __construct(Article $obj){
$this->content = $obj->content . '<br/>' . '編輯A新寫的內(nèi)容';
}
public function decorator(){
return $this->content;
}
}
class editor_B extends Article{
public function __construct(Article $obj){
$this->content = $obj->content . '<br/>' . '編輯B新寫的內(nèi)容';
}
public function decorator(){
return $this->content;
}
}
class editor_C extends Article{
public function __construct(Article $obj){
$this->content = $obj->content . '<br/>' . '編輯C新寫的內(nèi)容';
}
public function decorator(){
return $this->content;
}
}
$artCls = new Article('你好');
//編輯A先秀修改,然后編輯B修改,然后編輯C修改
$a = new editor_A($artCls);
$b = new editor_B($a);
$c = new editor_C($b);
echo $c->decorator();
//編輯B先秀修改,然后編輯A修改
$b = new editor_B($artCls);
$a = new editor_A($b);
echo $a->decorator();
//重點是傳遞參數(shù)的地方,使用Article $obj傳遞上一個操作的對象,
//來實現(xiàn)對同一個對象進行連續(xù)操作運行結(jié)果:
你好
編輯A新寫的內(nèi)容
編輯B新寫的內(nèi)容
編輯C新寫的內(nèi)容你好
編輯B新寫的內(nèi)容
編輯A新寫的內(nèi)容
上述就是小編為大家分享的怎么在php中應(yīng)用裝飾者模式了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:怎么在php中應(yīng)用裝飾者模式-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://chinadenli.net/article22/dspocc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、域名注冊、商城網(wǎng)站、全網(wǎng)營銷推廣、網(wǎng)站改版、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容