PHP's responsibility chain model for implementing design patterns

Source: Internet
Author: User
Tags class manager

<?php/** * Responsibility Chain Mode * * to unbind the sender and receiver of the request, and use multiple objects to handle the request, link the objects together and pass the request along this chain until there is an object that handles it * Abstract processor role: Defines an interface for processing requests. and a successor (optional) specific processor role: handles the request it is responsible for, can access the successor, and if it can handle the request, otherwise forwards the request to his successor. Customer class: Submits a request to a specific processor on a chain Concretehandler object.  * */* Responsibility chain mode is relatively flexible, the chain can be set to straight, ring can be * * Also have a pure responsibility chain, impure responsibility chain *///abstract processor role, generally contains two methods: processing the request, set the successor abstract class Handler{public for the request $next _handler;public function Setnexthandler ($handler) {$this->next_handler = $handler;} Abstract public Function executerequest ($request);} The specific processor, if able to handle, handle itself, cannot handle, leaving the next successor class Leader extends Handler{public function executerequest ($request) {if ($request- >days>0&& $request->days<=1) {echo ' team leader has approved ';} else{$this->next_handler->executerequest ($request);}} Class Director extends Handler{public function executerequest ($request) {if ($request->days>1&&$ request->days<=3) {echo ' director has approved ';} else{$this->next_handler->executerequest ($request);}} Class Manager extends Handler{public function executerequest ($requeST) {if ($request->days>3) {echo ' general manager has approved ';} else{$this->next_handler->executerequest ($request);}} Request Class Request{public $days;p ublic function __construct ($days) {$this->days = $days;}} Class Client{public static function Main1 () {//Constructs a request, three processing roles $request = new Request (2), $leadler = new Leader (); $director = n EW Director (); $manager = new manager ();//Make the three chains $leadler->setnexthandler ($director); $director->setnexthandler ($manager);//The chain head makes a request $leadler->executerequest ($request);} Public Function main2 () {$request = new request (2), $leadler = new Leader (); $director = new Director (); $manager = new Manage R ();//form a ring chain, who can be the recipient of $leadler->setnexthandler ($director); $director->setnexthandler ($manager); $ Manager->setnexthandler ($leadler); $manager->executerequest ($request);}} Client::main2 ();? >

The UML class diagram is as follows:

PHP's responsibility chain model for implementing design patterns

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.