Home > Backend Development > PHP Tutorial > Detailed explanation of PHP decorator pattern

Detailed explanation of PHP decorator pattern

小云云
Release: 2023-03-20 07:04:01
Original
2051 people have browsed it

Decorator mode Also called decorator mode. The decoration mode is to dynamically extend the functionality of an object without changing the original class file and using inheritance. It wraps the real object by creating a wrapping object, that is, decoration. This article mainly introduces the decorator pattern of PHP design pattern to everyone. It briefly introduces the concept and function of the decorator pattern and analyzes in detail the related operating skills of PHP implementation and use of the decorator pattern in the form of examples. Friends in need can refer to it. I hope Can help everyone.

UML Class Diagram:

##Role:

Interface of component objects: You can dynamically add responsibilities to these objects
The parent class of all decorators:You need to define an interface consistent with the component interface interface, and holds a Component object, which is actually the decorated object.
Specific decorator class: Implement the specific functions to be added to the decorated object. Used to decorate a specific component object or another specific decorator object.

Specific code:


<?php
/**
 * Created by PhpStorm.
 * User: Jiang
 * Date: 2015/5/3
 * Time: 11:11
 */
/**组件对象接口
 * Interface IComponent
 */
interface IComponent
{
  function Display();
}
/**待装饰对象
 * Class Person
 */
class Person implements IComponent
{
  private $name;
  function __construct($name)
  {
    $this->name=$name;
  }
  function Display()
  {
    echo "装扮的:{$this->name}<br/>";
  }
}
/**所有装饰器父类
 * Class Clothes
 */
class Clothes implements IComponent
{
  protected $component;
  function Decorate(IComponent $component)
  {
    $this->component=$component;
  }
  function Display()
  {
    if(!empty($this->component))
    {
      $this->component->Display();
    }
  }
}
//------------------------------具体装饰器----------------
class PiXie extends Clothes
{
  function Display()
  {
    echo "皮鞋 ";
    parent::Display();
  }
}
class QiuXie extends Clothes
{
  function Display()
  {
    echo "球鞋 ";
    parent::Display();
  }
}
class Tshirt extends Clothes
{
  function Display()
  {
    echo "T恤 ";
    parent::Display();
  }
}
class Waitao extends Clothes
{
  function Display()
  {
    echo "外套 ";
    parent::Display();
  }
}
Copy after login

Calling client test code:


header("Content-Type:text/html;charset=utf-8");
//------------------------装饰器模式测试代码------------------
require_once "./Decorator/Decorator.php";
$Yaoming=new Person("姚明");
$aTai=new Person("A泰斯特");
$pixie=new PiXie();
$waitao=new Waitao();
$pixie->Decorate($Yaoming);
$waitao->Decorate($pixie);
$waitao->Display();
echo "<hr/>";
$qiuxie=new QiuXie();
$tshirt=new Tshirt();
$qiuxie->Decorate($aTai);
$tshirt->Decorate($qiuxie);
$tshirt->Display();
Copy after login

Applicable scenarios:

1. It is necessary to dynamically add functions to an object, and these functions can be dynamically revoked.

2. 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.

3. 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.

Related recommendations:


Detailed explanation of the decorator pattern of PHP design pattern

The decorator pattern of Javascript and php and python Detailed explanation of comparative usage

Recommended articles about decorator

The above is the detailed content of Detailed explanation of PHP decorator pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template