java-design pattern (behavioral type)-"Responsibility chain Mode"

Source: Internet
Author: User

1.Chain of Responsibility (responsibility chain mode)

Definition: There are multiple objects, each holding a reference to the next object, so that a chain is formed , the request is passed on this chain,

Until an object decides to process the request. But the issuer does not know for sure that the object will eventually process the request.

One thing to emphasize here is that the request on the link can be a chain, a tree, or a ring, and the pattern itself does not constrain this,

We need to implement it ourselves, and at one point, the command allows only one object to be passed to another object, not to multiple objects.

Advantages: The responsibility chain model can be realized, in the case of concealing the client, the system is dynamically adjusted .

Reduce the coupling between classes, because there is no way to predict what type of request it is, and each class will not be able to handle it if it encounters it as long as it is discarded.

Cons: Inefficient because the completion of a request may be traversed to the last complete, and of course the tree's conceptual optimization can be used.

Diagram:

2. For example

Processor public interface Handler {    //processing method void operation ();} Chain node: Points to the next object public abstract class Abstracthandler {  private Handler handler=null, public Handler gethandler () { return handler;} public void SetHandler (Handler Handler) {this.handler = Handler;}} Processor implementation Class Class MyHandler extends Abstracthandler implements handler{    private string name;public MyHandler (String Name) {This.name=name;} @Overridepublic void operation () {System.out.println (name+) in the execution of the task ... ");    Call the next object to process if (GetHandler ()!=null) {Handler handler=gethandler (); Handler.operation ();} Else{system.out.println (name+ "Mission done! ");}}}

Test:

public class Test {public static void main (string[] args) {//TODO auto-generated method stub        MyHandler h1=new Myhandl ER ("Agent No. 1th");        MyHandler h2=new MyHandler ("Agent No. 2nd");        MyHandler h3=new MyHandler ("Agent No. 3rd");        Set the order of objects in the chain of responsibility        H1.sethandler (H2);        H2.sethandler (H3);        Starting from H1 to perform the task        h1.operation ();}}

Run:

Agent 1th, the mission. Agent 2nd, the mission. Agent 3rd, the mission. Agent 3rd, the mission is complete!
Pure and impure chain of Duty mode

A pure chain of responsibility requires a specific handler object to choose only one of two behaviors: one is to take responsibility, but to push the responsibility to the next. A situation in which a specific processor object is not allowed to transmit responsibility after taking part of the responsibility.

In a pure chain of responsibility, a request must be received by a handler object, and in an impure chain of responsibility, a request can eventually not be received by any of the receiving end objects.

The actual example of the pure responsibility chain model is difficult to find, and the examples we see are the implementation of the impure responsibility chain model. Some people think that the impure chain of responsibility is not the responsibility chain at all, which may make sense. But in a real system, a pure chain of responsibility is hard to find. If the adherence to the chain of responsibility is not the responsibility chain model, then the chain of responsibility model will not have much significance.

java-design pattern (behavioral type)-"Responsibility chain Mode"

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.