php设计模式--装饰器模式

包装对象 扩展实例。

interface IComponent
{
    function Display();
}

class Person implements IComponent
{
    private $name;
    function __construct($name){
        $this->name = $name;
    }
    function Display(){
        echo "装扮的:{$this->name}<br/>";
    }
}


class clothes implements IComponent
{
    protected $component;
    function Decorate(IComponent $component){
        $this->component = $component;
    }

    public function Display(){
        if (!empty($this->component)) {
            $this->component->Display();
        }
    }
}

class xie extends clothes
{
    function Display(){
        echo "回力";
        parent::Display();
    }
}

class yundong extends clothes 
{
    function Display(){
        echo "耐克";
        parent::Display();
    }
}

class txue extends clothes 
{
    function Display(){
        echo "阿迪";
        parent::Display();
    }
}

class waitao extends clothes 
{
    function Display(){
        echo "李宁";
        parent::Display();
    }
}

//$ym = new Person("姚明");
$md = new Person("麦迪");

//$xie = new xie();
//$waitao = new waitao();

//$xie->Decorate($ym);
//$waitao->Decorate($xie);
//$waitao->Display();
//echo "<hr/>";

$yd = new yundong();
$tx = new txue();

$yd->Decorate($md);
$tx->Decorate($yd);
$tx->Display();
die;
posted @ 2019-12-19 13:54  千载白云  阅读(184)  评论(0编辑  收藏  举报