微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐