Home >Backend Development >PHP Tutorial >PHP design pattern decorator pattern

PHP design pattern decorator pattern

高洛峰
高洛峰Original
2016-11-21 15:13:031159browse

Concept

The Decorator Pattern allows adding new functionality to an existing object without changing its structure. This type of design pattern is a structural pattern, which acts as a wrapper around an existing class.

This pattern creates a decoration class to wrap the original class and provide additional functionality while maintaining the integrity of the class method signature.

UML diagram

PHP design pattern decorator pattern

Role

Abstract component role (Component): Define an object interface to standardize objects that are prepared to accept additional responsibilities, that is, responsibilities can be dynamically added to these objects.

Concrete Component role (ConcreteComponent): The decorator defines a class that will be decorated to add functionality. You can add some responsibilities to objects of this class

Abstract decorator (Decorator): maintain an instance pointing to the component Component object, and define an interface consistent with the abstract component role Component interface

Concrete decorator role (ConcreteDecorator): Components add responsibilities.

Applicable scenarios

Need to dynamically add functions to an object, and these functions can be dynamically revoked.

It is necessary to add a very large number of functions generated by the permutation and combination of some basic functions, thus making the inheritance relationship unrealistic.

When the method of generating subclasses cannot be used for expansion. In one case, there may be a large number of independent extensions, and a large number of subclasses will be generated to support each combination, causing the number of subclasses to explode. Another case could be because the class definition is hidden, or the class definition cannot be used to generate subclasses.

Code

<?php
header(&#39;Content-type:text/html;charset=utf-8&#39;);

/**
 * 装饰器模式
 */

/**
 * Interface IComponent 组件对象接口
 */
interface IComponent
{
    public function display();
}

/**
 * Class Person 待装饰对象
 */
class Person implements IComponent
{
    private $_name;

    /**
     * Person constructor. 构造方法
     *
     * @param $name 对象人物名称
     */
    public function __construct($name)
    {
        $this->_name = $name;
    }

    /**
     * 实现接口方法
     */
    public function display()
    {
        echo "装扮者:{$this->_name}<br/>";
    }
}

/**
 * Class Clothes 所有装饰器父类-服装类
 */
class Clothes implements IComponent
{
    protected $component;

    /**
     * 接收装饰对象
     *
     * @param IComponent $component
     */
    public function decorate(IComponent $component)
    {
        $this->component = $component;
    }

    /**
     * 输出
     */
    public function display()
    {
        if(!empty($this->component))
        {
            $this->component->display();
        }
    }

}


/**
 * 下面为具体装饰器类
 */

/**
 * Class Sneaker 运动鞋
 */
class Sneaker extends Clothes
{
    public function display()
    {
        echo "运动鞋  ";
        parent::display();
    }
}

/**
 * Class Tshirt T恤
 */
class Tshirt extends Clothes
{
    public function display()
    {
        echo "T恤  ";
        parent::display();
    }
}

/**
 * Class Coat 外套
 */
class Coat extends Clothes
{
    public function display()
    {
        echo "外套  ";
        parent::display();
    }
}

/**
 * Class Trousers 裤子
 */
class Trousers extends Clothes
{
    public function display()
    {
        echo "裤子  ";
        parent::display();
    }
}


/**
 * 客户端测试代码
 */
class Client
{
    public static function test()
    {
        $zhangsan = new Person(&#39;张三&#39;);
        $lisi     = new Person(&#39;李四&#39;);

        $sneaker  = new Sneaker();
        $coat     = new Coat();

        $sneaker->decorate($zhangsan);
        $coat->decorate($sneaker);
        $coat->display();

        echo "<hr/>";

        $trousers = new Trousers();
        $tshirt   = new Tshirt();

        $trousers->decorate($lisi);
        $tshirt->decorate($trousers);
        $tshirt->display();
    }
}

Client::test();

Execution results:

Coat Sneakers Dresser: Zhang San
T-shirt Pants Dresser: Li Si


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn