Design Mode: chain of responsibility)

Source: Internet
Author: User

Chain of responsibility Definition
Chain of responsibility (CoR) processes a request using a series of classes. These classes are a discrete coupling, the only common point is to submit a request between them. That is to say, a request, Class A should not be handled first. If there is no handling, it should be resolved to Class B. If there is no handling, similar to the C operator, it is like a chain.

How to use it?
Although how to use cor in this section, it also demonstrates what cor is.

There is a handler interface:

Public interface handler {
Public void handlerequest ();
}

This is an example of processing requests. If there are multiple requests, for example, please help print or format them:

The first solution is to add more requests to the interface:
Public interface handler {
Public void handlehelp ();
Public void handleprint ();
Public void handleformat ();

}

The actual interface handler represents the following:
Public class concretehandler implements handler {
Private handler successor;

Public concretehandler (handler successor ){
This. Successor = successor;
}

Public void handlehelp (){
// Representative of the specific handling request for help
...
}

Public void handleprint (){
// Print the handler to process the print
Successor. handleprint ();
}
Public void handleformat (){
// Format processing
Successor. handleformat ();
}

}
There are a total of three practical differences. The above is the handling help, and the processing print processing format is probably our most commonly used programming idea.

Although we have a clear idea, we have a question. If we need to add another request type, we need to modify the interface and its implementation.

Solution 2: change each request to an interface, so we have the following generation:

Public interface helphandler {
Public void handlehelp ();
}

Public interface printhandler {
Public void handleprint ();
}

Public interface formathandler {
Public void handleformat ();
}

Public class concretehandler
Implements helphandler, printhandler, formathandlet {
Private helphandler helpsuccessor;
Private printhandler printsuccessor;
Private formathandler formatsuccessor;

Public concretehandler (helphandler helpsuccessor, printhandler printsuccessor, formathandler formatsuccessor)
{
This. helpsuccessor = helpsuccessor;
This. printsuccessor = printsuccessor;
This. formatsuccessor = formatsuccessor;
}

Public void handlehelp (){
.......
}

Public void handleprint () {This. printsuccessor = printsuccessor ;}

Public void handleformat () {This. formatsuccessor = formatsuccessor ;}

}

This method adds a new request response, which only saves the amount of introduced modifications. The concretehandler still needs to be modified in the interface. In addition, it is not easy to generate simple orders.

Solution 3: Only one linear data method is used in the handler interface:
Public interface handler {
Public void handlerequest (string request );
}
The actual handler generation is as follows:
Public class concretehandler implements handler {
Private handler successor;

Public concretehandler (handler successor ){
This. Successor = successor;
}

Public void handlerequest (string request ){
If (request. Equals ("help ")){
// This operator is the specific representative of the help processing tool.
} Else
// Upload data to the next
Successor. Handle (request );

}
}

}

Here, the request is a string type. If not, how can this problem be solved? Of course, we can create a response similar to a request.

The final solution is as follows:
Public interface handler {
Public void handlerequest (request );
}
Definitions of request types:
Public class request {
Private string type;

Public request (string type) {This. type = type ;}

Public String GetType () {return type ;}

Public void execute (){
// The request is actually a response
}
}
The actual handler generation is as follows:
Public class concretehandler implements handler {
Private handler successor;

Public concretehandler (handler successor ){
This. Successor = successor;
}

Public void handlerequest (request ){
If (request instanceof helprequest ){
// This operator is the specific representative of the help processing tool.
} Else if (request instanceof printrequst ){
Request.exe cute ();
} Else
// Upload data to the next
Successor. Handle (request );

}
}

}

This solution is Cor. in a region, there are different types of response, so it is calledChain of responsibility.

Summary of Cor:
Because it is impossible to tell which kind of type is the reason for external requests, if each kind does not meet the requirement that it cannot be handled, it is only necessary to put it into consideration. Undoubtedly, this reduces the coupling between different types.

The lack of efficiency is low, because the completion of a request may be completed only after it is completed. The concept of "complete" can also be used to compile it. In Java awt1.0, Cor is used to handle mouse clicks. After java.1.1, observer is used to replace cor.

Poor scalability, because in Cor, there must be a unified interface handler. The limitations are here.

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.