design mode (in)-----responsibility chain Model

Source: Internet
Author: User

Responsibility chain model (chain of responsibility) definition

The responsibility chain pattern is the behavior pattern of the object. Enables multiple objects to have the opportunity to process requests, thus avoiding direct coupling between the sender and the recipient of the request. Connect the objects as a chain and pass the request along this chain until an object has processed it.

UML Class Diagram

Role   Abstract Processor ( Handler) Role: Defines the interface for a request. If necessary, you can define a method to set and return a reference to a home object.   Specific Processor ( ConcretehandlerRole: If you can deal with the request, if not processing, the request passed to the homes, let the under processing. This means that it handles requests that it can handle and can access its own homes. Example

If someone wants to leave, team leader can only approve a vacation less than or equal to 1 days, the project manager can approve a vacation less than or equal to 3 days, and the CEO can approve all the holidays.

Handler

 Packagecom.csdhsm.designpattem.chainofresponsibility;/*** @Title: Handler.java * @Description: Approval of Leave abstract class *@author: Han * @date: June 29, 2016 PM 6:05:46*/   Public Abstract classHandler {protectedHandler Handler;  PublicHandler gethandler () {returnhandler; }     Public voidSetHandler (Handler Handler) { This. Handler =handler; }        //Approval Method     Public Abstract voidApproveintDay );}

Groupleader (team leader)

 Packagecom.csdhsm.designpattem.chainofresponsibility;/*** @Title: Groupleader.java * @Description: Team leader, can approve a leave request less than or equal to 1 days *@author: Han * @date: June 29, 2016 PM 5:58:29*/   Public classGroupleaderextendsHandler {@Override Public voidApproveintDay ) {        if(Day <= 1) {System.out.println ("Team leader Approval"); } Else{System.out.println ("Exceeding the team leader authority, handing over the superior");  This. GetHandler (). Approve (day); }    }}

Projectmanager (project manager)

 Packagecom.csdhsm.designpattem.chainofresponsibility;/*** @Title: Projectmanager.java * @Description: Project manager, can approve is less than equal to 3 days of leave *@author: Han * @date: June 29, 2016 PM 6:01:49*/   Public classProjectmanagerextendsHandler {@Override Public voidApproveintDay ) {        if(Day <= 3) {System.out.println ("Project manager Approval"); } Else{System.out.println ("Exceeding the project manager's authority and handing over to the superior");        GetHandler (). Approve (day); }    }}

Ceo

 Package com.csdhsm.designpattem.chainofresponsibility; /**    * @Title:  ceo.java    @author: Han    * @date:   June 29, 2016 PM 6:03:59     */public  classextends  Handler {    @Override     publicvoid approve (Int. Day ) {        System.out.println ("CEO approval") );    }}

Client

 Packagecom.csdhsm.designpattem.chainofresponsibility; Public classSolution { Public Static voidMain (string[] args) {Handler leader=NewGroupleader (); Handler Manager=NewProjectmanager (); Handler CEO=NewCEO (); //Connect Handler to a chainLeader.sethandler (manager);                Manager.sethandler (CEO); //Processing RequestsLeader.approve (6); }}

Results

OK, Success!

Summarize

 Advantages

The caller does not need to know who will handle the request, nor does it know the concrete structure of the chain, reduces the coupling degree of node domain node, and can dynamically modify the object responsibilities in the chain at runtime, which enhances the flexibility of assigning responsibilities to objects.

  Disadvantages

    Without a clear receiver, it may be passed to the end of the chain, and is not handled correctly.

design mode (in)-----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.