PHP設計模式之裝飾者模式

<?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();
相關文章
相關標籤/搜索