A brief talk on Java design pattern--The responsibility chain model (COR)

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/45569099
I. Overview

Enables multiple objects to have the opportunity to process requests, thus avoiding the coupling between the sender and receiver of the request. Link the objects together and pass the request along the chain until an object handles it. The idea of this model is to give multiple objects a chance to process a request, thus decoupling the sender and the recipient.

Second, applicability

1. There are multiple objects that can handle a request and which object handles the request when the runtime is automatically determined.

2. You want to submit a request to one of multiple objects without explicitly specifying the recipient.

3. A collection of objects that can handle a request should be specified dynamically.

Third, participants

1.Handler defines an interface for processing requests. (optional) To implement the successor chain.

2.ConcreteHandler handles the request it is responsible for. can access its successors. If the request can be processed, the request is forwarded to its successor.

3.Client submits a request to a specific processor (Concretehandler) object on the chain.

Four, class diagram

V. Examples

Handler

Package Com.lyz.design.cor;import org.omg.corba.request;/** * Handler * @author Liuyazhuang * */public interface Requesth andle {    void HandleRequest (Request request);}
Concretehandler

Package Com.lyz.design.cor;import org.omg.corba.request;/** * Concretehandler  * @author Liuyazhuang * */public Class Hrrequesthandle implements RequestHandle {public    void HandleRequest (Request request) {        if (request instanceof dimissionrequest) {            System.out.println ("to leave, personnel approval!");        }         SYSTEM.OUT.PRINTLN ("request Complete");}    }

Package com.lyz.design.cor;/** * Concretehandler * @author Liuyazhuang * */public class Pmrequesthandle implements Request Handle {    RequestHandle RH;        Public Pmrequesthandle (RequestHandle RH) {        THIS.RH = RH;    }        public void HandleRequest (Request request) {        if (Request instanceof Addmoneyrequest) {            System.out.println ("to raise , the project manager approves! ");        } else {            rh.handlerequest (request);}}}    

Package com.lyz.design.cor;/** * Concretehandler * @author Liuyazhuang * */public class Tlrequesthandle implements Request Handle {    RequestHandle RH;    Public Tlrequesthandle (RequestHandle RH) {        THIS.RH = RH;    }    public void HandleRequest (Request request) {        if (Request instanceof Leaverequest) {            System.out.println ("to leave, Project leader approval! ");        else {            rh.handlerequest (request);}}}    

Test
Package Com.lyz.design.cor;import org.omg.corba.request;/** * Test * @author Liuyazhuang * */public class Test {    publi c static void Main (string[] args) {        RequestHandle hr = new Hrrequesthandle ();        RequestHandle pm = new Pmrequesthandle (HR);        RequestHandle tl = new Tlrequesthandle (PM);                Team leader processing a request for separation requests        = new request ();        Tl.handlerequest (request);                System.out.println ("===========");        Team leader to handle the pay increase request        = new Addmoneyrequest ();        Tl.handlerequest (request);                System.out.println ("========");        Project manager Oberlie resignation request        = new Dimissionrequest ();        Pm.handlerequest (request);}    }

Result

To leave, Personnel approval! Request completed =========== to pay rise, project manager approval!======== to leave, personnel approval! Request Complete



A brief talk on Java design pattern--The responsibility chain model (COR)

Related Article

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.