Multilevel cache responsibility chain mode. * The client submits to hander,hander the function that can handle the task on the chain of discovery, processing, 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.
-
- /**
- * \ Responsibility chain mode, the purpose is to organize an object chain to process a request such as a method call.
- *
- * Best-known chain of responsibility Examples: multilevel caching.
- * The client submits to hander,hander the function that can handle the task on the responsibility chain, processing;
- * Can be summed up as: Using 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. In other words, a request is made, a class is processed first, and if not processed,
- * Passed to Class B processing, if not processed, passed to Class C processing, just like a chain (chain) passed down.
- */
- /**
- * The Handler abstraction. Objects that want to being a part of the
- * Chainofresponsibility must implement this interface directly or via
- * Inheritance from an abstracthandler.
- * Handling abstract classes, objects that must be implemented directly if they want to be part of the chain of responsibility
- * Inherit an abstract processing class
- */
- 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.
- * Receives a request, tries to satisfy it, and, if unsuccessful, delegates to the next handler.
- */
- 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.
- * Ideally, the last specific processing class on the chain of responsibility, added to the chain, will 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.
- * In the case of cache dead, forward to the next processing object
- */
- 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.
- * Do not care about the specific implementation of the external specific processing procedures; Behind the chain of responsibility.
- */
- 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.enbttel '
- )
- );
- 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";
- /**
- * Expect: ...
- * ISAAC Asimov
- * Helmut Hei.enbttel
- *
- 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.
- */
Copy Code |