The deep analysis _php skill of the responsibility chain model of PHP design pattern

Source: Internet
Author: User
The responsibility chain pattern, which is designed to organize an object chain to process a request such as a method call.
When Concretehandler (a specific handler) does not know how to satisfy a request from a client, or its purpose is not, it is delegated to the next handler (handler) in the chain to handle it.

This design pattern is typically used in conjunction with a composite pattern in which some leaves or container objects are delegated by default to their parent object. Another example is that localization is usually handled using the responsibility chain, and when the German translation adapter does not find the right result for the translated keyword, it returns to the English adapter or simply displays the keyword itself.

The coupling is minimized: The client class does not know which specific class to handle the request, and the chain is configured when the object graph is created; Concretehandlers does not know which object is their successor. Behavior is successfully assigned between objects, and the most recent object in the chain has priority and responsibility to satisfy the request.

Participants:
Client: Submits a request to the handler (handler);
Handler (handler) Abstraction: Receives a request, satisfies it in some way;
Concretehandlers (Specific handler): Receives a request, manages to satisfy it, and, if unsuccessful, delegates to the next handler.
The following code implements one of the most famous example of a responsibility chain: multilevel caching.
Copy Code code as follows:

/**
* The Handler abstraction. Objects that want to being a part of the
* Chainofresponsibility must implement this interface directly or via
* Inheritance from a abstracthandler.
*/
Interface Keyvaluestore
{
/**
* Obtain a value.
* @param string $key
* @return Mixed
*/
Public function get ($key);
}

/**
* Basic No-op Implementation which concretehandlers not interested in
* Caching or in interfering with the retrieval inherit from.
*/
Abstract class Abstractkeyvaluestore implements Keyvaluestore
{
protected $_nexthandler;

Public function Get ($key)
{
return $this->_nexthandler->get ($key);
}
}

/**
* Ideally the last concretehandler in the chain. At least, if inserted in
* A Chain it would be called.
*/
Class Slowstore implements Keyvaluestore
{
/**
* This could is a somewhat slow store:a database or a flat file.
*/
protected $_values;

Public function __construct (array $values = Array ())
{
$this->_values = $values;
}

Public function Get ($key)
{
return $this->_values[$key];
}
}

/**
* A concretehandler that handles the request for A key by looking for it
* its own cache. Forwards to the next handler in case of cache miss.
*/
Class Inmemorykeyvaluestore implements Keyvaluestore
{
protected $_nexthandler;
Protected $_cached = Array ();

Public function __construct (Keyvaluestore $nextHandler)
{
$this->_nexthandler = $nextHandler;
}

protected function _load ($key)
{
if (!isset ($this->_cached[$key])) {
$this->_cached[$key] = $this->_nexthandler->get ($key);
}
}

Public function Get ($key)
{
$this->_load ($key);
return $this->_cached[$key];
}
}

/**
* A Concretehandler that delegates the request without trying to
* Understand it at all. It may is easier to use in the user interface
* because it can specialize itself by defining methods that generates
* HTML, or by addressing similar user interface concerns.
* Some Clients the ' This object ' as an instance of Keyvaluestore
* And don't care how it satisfy their requests, while the other ones
* May use it entirety (similar to a class-based adapter).
* No client knows that a chain of handlers exists.
*/
Class Frontend extends Abstractkeyvaluestore
{
Public function __construct (Keyvaluestore $nextHandler)
{
$this->_nexthandler = $nextHandler;
}

Public Function getescaped ($key)
{
Return Htmlentities ($this->get ($key), ent_noquotes, ' UTF-8 ');
}
}

Client Code
$store = new Slowstore Array (' PD ' => ' Philip K. Dick '),
' ia ' => ' Isaac Asimov ',
' AC ' => ' Arthur c. Clarke ',
' hh ' => ' Helmut Heißenbüttel '));
In development, we skip cache and pass $store directly to Frontend
$cache = new Inmemorykeyvaluestore ($store);
$frontEnd = new Frontend ($cache);

echo $frontEnd->get (' ia '), "\ n";
echo $frontEnd->getescaped (' hh '), "\ n";

about the PHP responsibility chain design pattern of some implementation instructions:
The chain of responsibility may already exist in the object graph, as in the case of the composite pattern;
In addition, handler abstraction may or may not exist, the best choice is a separate handler interface can only perform handlerequest () operation, do not force a chain only in one level, because the following already exist;
It is also possible to introduce an abstract class, but because request processing is an orthogonal concern, the specific class may have inherited other classes;
By constructor or Setter,handler (or the next Handler) is injected into the client or the previous Handler;
The request object is usually a valueobject, or it may be implemented as a flyweight, in PHP it may be a scalar type, such as String, note that in some languages, a string is a constant valueobject.

A simple summary of the responsibility chain model can be summed up as: A series of classes (classes) trying to process a request requests, these classes are a loose coupling, the only thing in common is to pass request between them. In other words, a request, a class first processing, if not processed, passed to Class B processing, if not processed, passed to the C class processing, so like a chain (chain) as passed down.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.