1
<?php
#houdunwang.com 后盾人 人人做后盾
#houdunren.com
namespace app;

//定义装饰对象/装饰器规范的接口
interface Component
{
    public function display();
}

//被装饰者
class Person implements Component
{
    public function display()
    {
        echo '<br/>后盾网  www.houdunwang.com';
    }
}

//抽象装饰器: 维护装饰链条的抽象类
abstract class Decorate implements Component
{
    protected $componet;

    public function __construct(Component $component)
    {
        $this->componet = $component;
    }

    public function display()
    {
        if ( ! is_null($this->componet)) {
            $this->componet->display();
        }
    }
}

//装饰器: 用于装饰被装饰者
class Car extends Decorate
{
    public function display()
    {
        echo "<br/>i have a car";
        parent::display(); // TODO: Change the autogenerated stub
    }

}

//装饰器
class Bus extends Decorate
{
    public function display()
    {
        echo "<br/> I has a Bus";
        parent::display();
    }

}

$person = new Person();
$car    = new Car($person);
$bus    = new Bus($car);
$bus->display();

后盾网向军
26 声望6 粉丝

HDPHP&HDCMS作者