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

Detailed explanation of using PHP decorator pattern

May 17, 2018 am 11:48 AM
php use Detailed explanation

This time I will bring you PHPDecorator modeDetailed explanation of use, what are the precautions for using PHP decorator mode, the following is a practical case, let's take a look.

What is the decorator pattern

As a structural pattern, the decorator pattern is to add " Decoration".

Adapter pattern is to add an adapter class to the current structure. It converts the interface of one class into another one that the customer expects. Interfaces and adapters allow classes with incompatible interfaces to work well together.

Decorator pattern is to wrap an object to enhance new behaviors and responsibilities. Decorators are also called Wrapper (similar to adapter)

Some design patterns include an abstract class, and the abstract class also inherits another abstract class. There are only a few such design patterns, and decorator is one of them.

When to use the decorator pattern

Basically speaking, if you want to add new functionality to an existing object without affecting other objects, just You can use the Decorator Pattern. If you've gone to great lengths to create a website format for a client, and the main components are working perfectly, you don't want to have to reinvent the wheel and recreate the website when the client requests a new feature. For example, let's say you've After building the components originally requested by the customer, the customer has new needs and wants to include video functionality in the website. You don't need to rewrite the original components, you only need to "decorate" the existing components to add video functionality to them. That's it You can maintain the original functions, and you can also add new functions.

Some projects may sometimes need to be decorated, and sometimes do not want to be decorated. These projects reflect another important feature of the decorator design pattern. Assume that your basic website Development mode meets the requirements of most customers. However, some customers also want some specific features to meet their needs. Not everyone wants or needs these additional features. As a developer, you want what you create The website can meet the customer's business goals. Therefore, it needs to provide "customerization" features, that is, features provided for specific businesses. Using the decorator pattern, not only can it provide core functions, but it can also be "decorated" with unique functions required by customers. These core functions.

Simple Decorator Example

A web development company plans to build a basic website with some enhanced features. However, web developers know , although this basic plan works for most customers, it is likely that customers will want to improve further in the future. Using the decorator mode, you can easily add multiple specific decorators. In addition, because you can choose which decorators to add, enterprises Not only can the function be controlled, but also the cost of the project can be controlled.

Component interface

The Component participant is an interface, here, it is an abstract class IComponent. This The abstract class has only one attribute $site, and two abstract methods getSite() and getPrice().Component Participants specifically establish interfaces for specific components and the Decorator participant abstract class:

IComponent.php

<?php
abstract class IComponent
{
  protected $site;
  abstract public function getSite();
  abstract public function getPrice();
}
Copy after login

Decorator interface

The decorator interface in this example may surprise you. This It is an abstract class, and it also extends another abstract class! The function of this class is to maintain a reference to the component interface (IComponent), which is done by extending IComponent:

Decorator.php

<?php
abstract class Decorator extends IComponent
{
  /*
  任务是维护Component的引用
  继承getSite()和getPrice()
  因为仍然是抽象类,所以不需要实现父类任何一个抽象方法
  */
}
Copy after login

The main function of the Decorator class is to maintain a reference to the component interface.

In all implementations of the decorator pattern, you will find that the specific components and decorators are the same interface. Their implementations may be different, and in addition to the properties and methods of the basic interface, components and decorators may have additional properties and methods.

Specific components

There is only one specific component in this example, which generates a website name and a basic website quote:

BasicSite.php

<?php
class BasicSite extends IComponent
{
  public function construct()
  {
    $this->site = "Basic Site";
  }
  public function getSite()
  {
    return $this->site;
  }
  public function getPrice()
  {
    return 1200;
  }
}
Copy after login

Both abstract methods are implemented using direct assignment, but the flexibility is not reflected in how to change the set value. In fact, the "Basic Site" value needs to be changed by adding a decorator value.

Specific decorator

这个例子中的具体装饰器与具体组件有相同的接口.实际上, 它们是从Decorator抽象类(而不是IComponent类)继承了这个接口. 不过,要记住, Decorator所做的就是继承IComponent接口.

Maintenance.php

<?php
class Maintenance extends Decorator
{
  public function construct(IComponent $siteNow)
  {
    $this->site = $siteNow;
  }
  public function getSite()
  {
    $format = "<br /> Maintenance";
    return $this->site->getSite() . $format;
  }
  public function getPrice()
  {
    return 950 + $this->site->getPrice();
  }
}
Copy after login

这个装饰器Maintenance在改变了site的值, 还有包装的具体组件价格上还会增加它自己 的价格. 另个两个具体装饰器与Maintenance装饰器也类似

Video.php

<?php
class Video extends Decorator
{
  public function construct(IComponent $siteNow)
  {
    $this->site = $siteNow;
  }
  public function getSite()
  {
    $format = "<br /> Video";
    return $this->site->getSite() . $format;
  }
  public function getPrice()
  {
    return 350 + $this->site->getPrice();
  }
}
Copy after login

DataBase.php

<?php
class DataBase extends Decorator
{
  public function construct(IComponent $siteNow)
  {
    $this->site = $siteNow;
  }
  public function getSite()
  {
    $format = "<br /> DataBase";
    return $this->site->getSite() . $format;
  }
  public function getPrice()
  {
    return 800 + $this->site->getPrice();
  }
}
Copy after login

测试这个应用时,可以看到,在基本的价格之上还会增加各个装饰器的价格.另外还能指定装饰器名的格式, 增加了两个空格,使之缩进

装饰器实现中最重要的元素之五就是构造函数, 要为构造函数提供一个组件类型. 由于这里只有一个具体组件, 所有装饰器的实例化都会使用这个组件. 使用多个组件时, 装饰器可以包装应用中的一部分或全部组件, 也可以不包装任何组件.

客户

Client类并不是这个设计模式的一部分, 但是正确使用Client类至关重要.每个装饰器在实例化时"包装"组件, 不过, 首先必须创建一个要包装的对象, 这里是BasicSite类实例

Client.php

<?php
function autoload($class_name)
{
  include $class_name . &#39;.php&#39;;
}
class Client
{
  private $basicSite;
  public function construct()
  {
    $this->basicSite = new BasicSite();
    $this->basicSite = $this->WrapComponent($this->basicSite);
    $siteShow = $this->basicSite->getSite();
    $format = "<br /> <strong>Total= $";
    $price = $this->basicSite->getPrice();
    echo $siteShow . $format . $price . "</strong>";
  }
  private function WrapComponent(IComponent $component)
  {
    $component = new Maintenance($component);
    $component = new Video($component);
    $component = new DataBase($component);
    return $component;
  }
}
$worker = new Client();
Copy after login

wrapComponent()方法检查传入的BasicSite实例, 以确保参数有正确的数据类型(IComponent), 然后分别实例化3个装饰器, 对该实例对象进行装饰.

Basic Site
  Maintenance
  Video
  DataBase
  Total= $3300

适配器和装饰器模式都有另外一个名字"包装器"(wrapper)".

适配器可以"包装"一个对象, 创建一个与Adaptee兼容的接口, 而无须对它做任何修改.

装饰器也可以"包装"一个组件对象, 这样就能为这个已胡的组件增加职责, 而无须对它做任何修改.

下面的代码展示了Client如何将组件对象($component)包装在装饰器(Maintence)中:

$component = new Maintenance($component);
Copy after login

类似于"接口", 在计算机编程中用到"包装器"时, 不同的上下文会有不同的用法和含义. 一般来讲, 在设计模式中使用"包装器"是为了处理接口的不兼容, 或者希望为组件增加功能,包装器就表示用来减少不兼容性的策略.

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

PHP基于面向对象实现留言本步骤详解

PHP里氏替换原则实战分析

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles