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新寫的內容';
}
public function decorator(){
return $this->content;
}
}
class editor_B extends Article{
public function __construct(Article $obj){
$this->content = $obj->content . 'br/>' . '編輯B新寫的內容';
}
public function decorator(){
return $this->content;
}
}
class editor_C extends Article{
public function __construct(Article $obj){
$this->content = $obj->content . 'br/>' . '編輯C新寫的內容';
}
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();
//重點是傳遞參數的地方,使用Article $obj傳遞上一個操作的對象,
//來實現對同一個對象進行連續操作