java - What are the advantages of chain-of-responsibility vs. lists of classes? -


Recently, I would like to refactor a huge (1000 lines) method filled with "if" statement with another programmer Was the best way .

The code is written in Java, but I think this problem can be with other languages ​​such as C #.

To solve this problem, he suggested using a series-Ledge pattern to propose a base "handler" class. After this, "Handler 1", "Handler 2", Etc. will extend "handler".
Then, handlers would have a "recipient" method, which would either return to the tap (if it was the final series) or the next chain handler.
Then, a "handlereequest (request)" function will either deal with the request, or give it to the next part of the series, and if none of the previous solutions will work, then it will only be empty or thrower An exception.
To add a new handler to the series, the encoder will go to the last element of the series and tell it that there was a new element. To do something, he used to call handle only on the first element of the chain.

To solve this problem, I suggested using a different approach.
I will have a base "handler" class as well as with "handler 1", "handler 2", as mentioned earlier.
However, there will be no "recipients" method. Instead, I have a collection class that has a list of handlers (a vector, an ArrayList, or whatever is best in this case).
handlest function will still exist, but this call will not be broadcast next operator will only process it on the request or return to the empty space.
To handle the request, anybody will use (handler handles: handler) for

  {result = handle.handleRequest (request); If (result! = Null) return result; } New CanonParseRequestException (); // Just like another approach  

Or, to prevent code duplication, a "parse request (request)" method can be added to the archive class. To add a new handler, a collection constructor (or static {} block, or some grouped) goes on and just "addHandler (new handler 3 ())"; ".

Adds the code. Chain's responsibility is I missing from this point? Which method is best (assuming there is a best method?) Why do not you?

  if (x is the type of example 1) {/ p> 

// doSomething1} and if (x for example 2) {// doSomething2} // I like my idea with better collection than those heirs. I

This set of easy and easy operators Manipulate: The archive interface is well known and everyone understands that there is no list or what not.

If you use this henchanger recommended by a friend, a lot Deep recurring (unless your platform supports tail calls, do not pay attention) I do not know if JVM is enabled.

I do not recommend adding any collection methods . you Fie is hard to difficult to meet complex design that understand and modify. There are two different concerns: a set of handler as a chain of responsibility and Save the interpretation of the handler. A method that handles the request by iterating on a collection is at the higher level of intangible compared to the collection house system, so the collection should not be related to the interface.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -