The responsibility chain model, whose purpose is 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 the client, or if it is not intended for this purpose, it is delegated to the next handler (handler) in the chain to process.
This design pattern is typically used in conjunction with composite patterns, where some leaves or container objects default to delegating operations to their parent objects. 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 translation keyword, it returns to the English adapter or simply displays the keyword itself directly.
The coupling is minimized: The client class does not know which specific class to handle the request, the chain is configured when the object graph is created, and Concretehandlers does not know which object is their successor. The behavior between objects is successful, and the closest 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 to satisfy it in some way;
Concretehandlers (Specific handler): Receives a request, tries to satisfy it, and, if unsuccessful, delegates to the next handler.
The following code implements one of the most famous examples of the chain of responsibility: multilevel caching.
Copy CodeThe code is as follows:
/**
* The Handler abstraction. Objects that want to being a part of the
* Chainofresponsibility must implement this interface directly or via
* Inheritance from an 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'll be, the last node to 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 in
* 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 the user interface
* because it can specialize itself by defining methods that generates
* HTML, or by addressing similar user interface concerns.
* Some clients see this object only as an instance of Keyvaluestore
* And do not care about it satisfy their requests, while other ones
* May use it on 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 ';
Some implementation instructions for the PHP liability chain design Pattern:
The chain of responsibility may already exist in the object graph, as is the case with 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 latter already exists;
It is possible to introduce an abstract class, but because request processing is an orthogonal concern, the specific class may have inherited other classes;
is injected into the client or the previous Handler by constructor or Setter,handler (or the next Handler);
The request object is usually a valueobject, or can 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.
The simple summary of the chain of responsibility model can be summed up as: A series of classes (classes) to try to handle a request requests, these classes are a loose coupling, the only thing in common is to pass the request between them. That is, a request, a class first processing, if not processed, is passed to Class B processing, if not processed, passed to the Class C processing, just like a chain (chain) passed down.
http://www.bkjia.com/PHPjc/327557.html www.bkjia.com true http://www.bkjia.com/PHPjc/327557.html techarticle The responsibility chain model, whose purpose is 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 the client, ...