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;

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...