這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)怎么在php中應(yīng)用裝飾者模式,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
裝飾模式指的是在不必改變?cè)愇募褪褂美^承的情況下,動(dòng)態(tài)地?cái)U(kuò)展一個(gè)對(duì)象的功能。它是通過創(chuàng)建一個(gè)包裝對(duì)象,也就是裝飾來包裹真實(shí)的對(duì)象。
示例:
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(); //重點(diǎn)是傳遞參數(shù)的地方,使用Article $obj傳遞上一個(gè)操作的對(duì)象, //來實(shí)現(xiàn)對(duì)同一個(gè)對(duì)象進(jìn)行連續(xù)操作
運(yùn)行結(jié)果:
你好
編輯A新寫的內(nèi)容
編輯B新寫的內(nèi)容
編輯C新寫的內(nèi)容你好
編輯B新寫的內(nèi)容
編輯A新寫的內(nèi)容
上述就是小編為大家分享的怎么在php中應(yīng)用裝飾者模式了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:怎么在php中應(yīng)用裝飾者模式-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://aaarwkj.com/article22/dspocc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、域名注冊(cè)、商城網(wǎng)站、全網(wǎng)營銷推廣、網(wǎng)站改版、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容