Detailed explanation of using PHP decorator pattern

php中世界最好的语言
Release: 2023-03-26 09:26:01
Original
2102 people have browsed it

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!