首页 > Web开发 > 详细

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

时间:2019-12-19 14:36:22      阅读:71      评论:0      收藏:0      [点我收藏+]

包装对象 扩展实例。

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;

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

原文:https://www.cnblogs.com/songyanan/p/12067341.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!