Design pattern----Chain of responsibility (duty chain) mode

Source: Internet
Author: User
Design Patterns ----Chain of responsibility ( Responsibility Chain ) Mode GOF :Enables multiple objects to have the opportunity to process requests, thereby avoiding coupling between the sender and receiver of the request.   Connect the objects into a chain and pass the request along the chain until an object handles it. Chain of responsibility (responsibility chain) mode is actually quite simple. In layman's terms, when a request is sent to me, I can deal with it, and I can't handle it and push it to someone else to handle it. That is, the object that accepts the request forms a chain, and the request is passed on the chain until an object on the chain decides to process the request. The customer who issued the request can dynamically rearrange the chain and assign responsibility without affecting the client.   Can also understand this, you go to a place to do a thing, originally very small things, but the people shifting, a push one, finally have a heart good person to do things to you. The following is a simple example: The following is a UML diagram: reference: GOF "design mode" by GOF "" in Patterns "by Bruce Eckel" design mode "by Banqiao people http://zhenyulu.cnblogs.com/ articles/65850.html package chainofresponsibility;

Public abstract class Handler
... {
protected Handler successor;

public void Setsuccessor (Handler successor)
... {
This.successor = successor;
}//end setsuccessor (...)

Abstract public void HandleRequest (int request);

}//End abstract class Handler

Package chainofresponsibility;

public class ConcreteHandler1 extends Handler
... {
public void HandleRequest (int request)
... {
if (Request >= 0 && Request < 10) ... {
System.out.println (this);
SYSTEM.OUT.PRINTLN ("Handled request" + request);
}else ... {
if (successor!= NULL)
Successor.handlerequest (Request);
}
}//end handlerequest (...)

}//End Class ConcreteHandler1

Package chainofresponsibility;

public class ConcreteHandler2 extends Handler
... {
public void HandleRequest (int request)
... {
if (Request >= && request < 20) ... {
System.out.println (this);
SYSTEM.OUT.PRINTLN ("Handled request" + request);
}else ... {
if (successor!= NULL)
Successor.handlerequest (Request);
}
}//end handlerequest (...)

}//End Class ConcreteHandler2

Package chainofresponsibility;

public class ConcreteHandler3 extends Handler
... {
public void HandleRequest (int request)
... {
if (Request >= && request < 30) ... {
System.out.println (this);
SYSTEM.OUT.PRINTLN ("Handled request" + request);
}else
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.