Detailed Explanation of Decoration Pattern Application Case of php Design Pattern

  • 2021-12-11 17:33:30
  • OfStack

In this paper, the decoration mode of php design mode is described as an example. Share it for your reference, as follows:

Introduction

The Decorator pattern (Decorator Pattern) allows you to add new functionality to an existing object without changing its structure. This type of design pattern is a structured pattern, which is a wrapper for an existing class. This pattern creates a decorative class, which is used to wrap the original class, and provides additional functions while maintaining the integrity of the class method signature.

Main role

Abstract Component (Component) Role: Defines an exclusive interface that specifies objects prepared to receive additional responsibilities so that responsibilities can be dynamically added to these objects. Concrete artifact (Concrete Component) role: Defines a class that will receive additional responsibilities. Decoration (Decorator) role: Hold a pointer to an Component object and define an interface to Component interface 1. Specific decoration (Concrete Decorator) role: Responsible for adding additional responsibilities to component objects.

Here is a simple implementation using decorative patterns:


class RequestHelper{}
abstract class ProcessRequest{
  abstract function process(RequestHelper $req);
}
class MainProcess extends ProcessRequest{
  function process(RequestHelper $req)
  {
    print __CLASS__.": doing something useful with request\n";
  }
}
abstract class DecorateProcess extends ProcessRequest{
  protected $processRequest;
  function __construct(ProcessRequest $pr)
  {
    $this->processRequest = $pr;
  }
}

As before, we define an abstract base class (ProcessRequest), a concrete component (MainProcess), and an abstract decorator class (DecorateProcess). The MainProcess:: process () method simply reports that the method has been called and has no other functionality. DecorateProcess saves an ProcessRequest object for its subclass. Here are some simple concrete decoration categories:


class LogRequest extends DecorateProcess{
  function process(RequestHelper $req)
  {
    print __CLASS__.": logging request\n";
    $this->processRequest->process($req);
  }
}
class AuthenticateRequest extends DecorateProcess{
  function process(RequestHelper $req)
  {
    print __CLASS__.": authenticating request\n";
    $this->processRequest->process($req);
  }
}
class StructureRequest extends DecorateProcess{
  function process(RequestHelper $req)
  {
    print __CLASS__.": structuring request\n";
    $this->processRequest->process($req);
  }
}

Every process () method of the decorating class outputs one message before calling the Process () method of the referenced processRequest object.

Now we can merge the objects of these classes at run time, creating filters to perform different actions on each 1 request in a different order. The following code combines the objects of all concrete classes into one filter:


$process = new AuthenticateRequest(new StructureRequest(
  new LogRequest(
    new MainProcess()
  )));
$process->process(new RequestHelper());

Executing the code will result in the following output:

Authenticate
Request: authenticating request
StructureRequest: structuring request
LogRequest: logging request
MainProcess: doing something useful with request

Advantages:

Decorative class and decorated class can develop independently without coupling with each other. Decorative mode is an inherited substitute mode, and decorative mode can dynamically extend the functions of an implementation class.

Disadvantages:

Multi-layer decoration is more responsible.

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: