Multi-level caching chain of responsibility model.
* The client submits it to the handler, and the handler finds a function in the chain of responsibility that can handle the task and processes it; it can be summarized as: using a series of classes to try to handle a request. There is a loose coupling between these classes, and the only thing they have in common is The point is to pass the request between them. In other words, when a request comes, Class A will handle it first. If it is not processed, it will be passed to Class B for processing. If it is not processed, it will be passed to Class C for processing. It is like a chain. (chain) is passed on.
- /**
- * Chain of Responsibility pattern, its purpose is to organize an object chain to handle a request such as a method call.
- *
- * The most famous example of chain of responsibility: multi-level caching.
- * The client submits it to the handler, and the handler finds a function in the chain of responsibility that can handle the task and processes it;
- * It can be summarized as: using a series of classes to try to handle a request, and there is a loose coupling between these classes.
- * The only thing they have in common is that requests are passed between them. In other words, when a request comes, Class A will handle it first. If it is not processed,
- * will be passed to Class B for processing. If it is not processed, it will be passed to Class C for processing. , and it is passed on like a chain.
- */
-
-
- /**
- * The Handler abstraction. Objects that want to be a part of the
- * ChainOfResponsibility must implement this interface directly or via
- * inheritance from an AbstractHandler.
- * Interface or
- * 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 delegates to the next handler if unsuccessful.
- */
- 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 will be the last node to be called.
- * Ideally, the last concrete processing class on the responsibility chain is added to the chain and will is the last node called.
- */
- class SlowStore implements KeyValueStore{
- /**
- * This could be 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 case of cache miss, 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 be 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 see this object only as an instance of KeyValueStore
- * and do not care how it satisfy their requests, while other ones
- * may use it in its 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.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 (client): Submit a request to Handler (handler);
- ◆Handler (handler) Abstract: Receive a request, satisfy it in some way;
- ◆ConcreteHandlers (concrete handlers): Receive a request, try to satisfy it, and delegate to the next handler if unsuccessful.
- */
-
复制代码
|