PHP设计模式之:装饰模式

时间:2023-11-23 16:21:26

<?php
// 人类
class Person
{
    private $name;
    public function __construct($name)
    {
        $this->name = $name;
    }

public function Show()
    {
        echo "装扮" . $this->name;
    }
}

//服饰类
class Finery extends Person
{
    protected component;

public function Decoration(Person $component)
    {
        $this->component = $component;
    }

public function Show()
    {
        if($this->component != null)
        {
            $this->component->Show();
        }
    }
}

// 具体服饰类
class TShirts extends Finery
{
    public function Show()
    {
        echo "T 恤";
        $this->Show();
    }
}

class BigTrouser extends Finery
{
    public function Show()
    {
        echo "大裤";
        $this->Show();
    }
}

class Suit extends Finery
{
    public function Show()
    {
        echo "西装";
        $this->Show();
    }
}

$p = new Person("狗娘养的");

$bt = new BigTrouser();
$bt.Decoration($p);
$bt.Show();

PHP设计模式之:装饰模式