装饰器模式
装饰器模式: 动态地给一个对象添加一些额外的职责或者行为。就增加功能来说, Decorator模式相比生成子类更为灵活。
类结构图:

php代码示例:
<?php
class Person{
    protected $name;
    function __construct($tname){
        $this->name = $tname;
    }
    function Show(){
        print "dressed ".($this->name)."\n";
    }
}
class Finery extends Person{
    protected $componet=null;
    function __construct(){
    }
    function Decorate($ct){
        $this -> componet = $ct;
    }
    function Show(){
        if(!is_null($this -> componet)){
            $this -> componet -> Show();
        }
    }
}
class TShirts extends Finery{
    function Show(){
        print "Big T-shirt\n";
        $this -> componet -> Show();
    }
}
class BigTrouser extends Finery{
    function Show(){
        print "Big Trouser ";
        $this -> componet -> Show();
    }
}
$p = new Person("Brown");
$bt = new BigTrouser();
//$ts = new TShirts();
$bt -> Decorate($p);
$bt -> Show();
//$ts->Decorate($bt);
//$ts->Show();
?>
 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号