PHP decorator pattern use case analysis

php中世界最好的语言
Release: 2023-03-26 19:04:02
Original
1529 people have browsed it

This time I will bring you PHPDecorator modeUse case analysis, what are the Notes when using PHP decorator mode, the following is a practical case, let's take a look.

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.

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 that is 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.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps to use the redis counter class

How to obtain the real-time CPU memory usage in PHP in Windows

The above is the detailed content of PHP decorator pattern use case analysis. 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