亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php裝飾者模式簡(jiǎn)單應(yīng)用案例分析

 更新時(shí)間:2019年10月23日 10:23:31   作者:學(xué)知無涯  
這篇文章主要介紹了php裝飾者模式簡(jiǎn)單應(yīng)用,結(jié)合具體實(shí)例形式分析了php裝飾者模式的原理及文章編輯相關(guān)應(yīng)用操作技巧,需要的朋友可以參考下

本文實(shí)例講述了php裝飾者模式簡(jiǎn)單應(yīng)用。分享給大家供大家參考,具體如下:

裝飾模式指的是在不必改變?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)容

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論