Responsibility chain Model

Source: Internet
Author: User

The responsibility chain pattern links the requested object to a chain, passing the request along the chain until there is an object processing the request, which allows multiple objects to have the opportunity to process the request, thus avoiding the coupling between the sender and the recipient of the request.
The responsibility chain pattern in the reality uses many, common is the OA system the workflow.

The main advantage of the responsibility chain model is that it can reduce the coupling degree of the system, simplify the mutual connection of objects, and enhance the flexibility of assigning responsibilities to objects, and it is convenient to add new request processing classes.
Its main disadvantage is that the request must not be guaranteed to be received, and for a longer chain of responsibility, the processing of the request may involve multiple processing objects, system performance will be affected, and the code debugging is not very convenient.

1<?PHP2 3 /**4 * We create abstract class Abstractlogger with verbose logging levels. We then created three types of loggers, all of which expanded the Abstractlogger. 5 * The level of each logger message is at its own level, and if it is printed accordingly, it will not print and pass the message to the next logger. 6  */7 8 Abstract classAbstractlogger9 {Ten      Public Static $info= 1; One      Public Static $debug= 2; A      Public Static $error= 3; -  -     protected $_level; the     protected $_nextlogger;//Important Concept -  -  -      Public functionSetnextlogger (\abstractlogger$abstractLogger) +     { -         $this->_nextlogger =$abstractLogger; +     } A  at      Public functionLogMessage ($level,$message) -     { -         if($this->_level = =$level) { -             $this->write ($message); -         } -  in         if($this-_nextlogger) { -             $this->_nextlogger->logmessage ($level,$message); to         } +     } -  the  *     Abstract protected functionWrite$msg); $ }Panax Notoginseng  -  the  + classConsoleloggerextendsAbstractlogger A { the      Public function__construct ($level) +     { -         $this->_level =$level; $     } $  -     protected functionWrite$msg) -     { the         Echo"<br/>standard Console::logger:".$msg; -     }Wuyi } the  - classErrorloggerextendsAbstractlogger Wu { -      Public function__construct ($level) About     { $         $this->_level =$level; -     } -  -     protected functionWrite$msg) A     { +         Echo"<br/>error Console::logger:".$msg; the     } - } $  the classFileLoggerextendsAbstractlogger the { the      Public function__construct ($level) the     { -         $this->_level =$level; in     } the  the     protected functionWrite$msg) About     { the         Echo"<br/>file Console::logger:".$msg; the     } the } +  -  the Bayi  the $errorLogger=NewErrorlogger (Abstractlogger::$error); the $fileLogger=NewFileLogger (Abstractlogger::$debug); - $consoleLogger=NewConsolelogger (Abstractlogger::$info); -  the $consoleLogger->setnextlogger ($fileLogger); the $fileLogger->setnextlogger ($errorLogger); the  the $chain=$consoleLogger; -  the  the $chain->logmessage (Abstractlogger::$info, "This was an information."); the $chain->logmessage (Abstractlogger::$debug, "This is a debug level information.");94 $chain->logmessage (Abstractlogger::$error, "This was an error information.");
View Code

Responsibility chain Model

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.