File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes What Design Pattern should I use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "What Design Pattern should I use?" Watch "What Design Pattern should I use?" New topic
Author

What Design Pattern should I use?

Eyal Golan
Greenhorn

Joined: May 20, 2008
Posts: 21
Hi,
I have a code that I want to refactor to be better maintainable and readable.
The code is like a sequence of operation.
Each operation return result, if the result is XXX, then doing the next operation.

I developed the code quick and dirty to check things, and now I want to make it better.
The current solution takes consideration only in success sequence.

The code is something like that:

Result r1 = app.doA1();
if (r1.status==1) {
Result r2 = app.doA2();

etc.


I am going to extract all the 'doA#' methods into classes.
Each class will process and return the next class or, if error, then will trigger a method from a centralized class.

After this long intro
I am sure there is a suitable design pattern for that (State???)
I want to build the code correctly.

Any suggestion will be gladly taken.

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Chain-of-responsibility
Eyal Golan
Greenhorn

Joined: May 20, 2008
Posts: 21
Thanks David,
but I'm not sure though.

You see, I have well, "conditional chain of responsibility".

Maybe I my DP is a mix of chain and state...
Andrey Choe
Greenhorn

Joined: Sep 07, 2010
Posts: 12
Hello,
it's hard to give exact advice without more code and details about domain objects and business logic. I can give an advice to extract class hierarchy from "app" an maybe hide it behind a factory.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1007
    
    3
Eyal Golan wrote:Thanks David,
but I'm not sure though.

You see, I have well, "conditional chain of responsibility".

Maybe I my DP is a mix of chain and state...


The standard Chain of Responsibility pattern is perfectly reasonable given your brief description of the situation. That pattern includes functionality where the request to handle an event/message is passed "up the line" iff the current object doesn't handle it. i.e. The conditionality is part of the pattern.

K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
One:
STATE Pattern is not at all a option.

State pattern has much more complexities...
it supports to non sequential invocation of the Operations based on Objects status....

you need to Wire you state Object with Observers object to attain it.....
i think you really need not require it

Second :
The best def i got for ChainOf Responsibility is:

"Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

considering this in mind, in your case,

the control of invocation of series nodes is in SINGLE calling class ...

Either you have to change this to something like

Requester --- > chain node 1 --- chain Node 2 ...... --- Receiver

but this has few corns as welll,
one of it is:: any structure change in Request Object MAY impact re-touching all nodes...

OR

In case you want to retain the control in single class but still want to modularize it may think od Delegate


gimme more a bit insight on your requirement, we might be missing something


“The difference between 'involvement' and 'commitment' is like an eggs-and-ham breakfast: the chicken was 'involved' - the pig was 'committed'.”
Eyal Golan
Greenhorn

Joined: May 20, 2008
Posts: 21
OK,
I guess I need to clarify things.
What I am actually trying to do is a sequence of calls to the web.
I am using Apache HttpClient library.

The purpose of the module is to simulate a sequence of calls (GET and POST).

So the sequence is something like:
Call a URL and parse the result.
If status result is 200 - call URL_A
else if status 302- call URL_B
else finish with error

Then, the same procedure (probably with different if / else) for URL_A
etc.

My solution was:
Each call to URL (I know exactly the options that will be in the sequence) is represented in a class.
All of them inherit a base abstract class. they have a method 'execute'.
In the execute, each class execute the POST (or GET) , parses the response and create the next class accordingly.

There is a Manager class with 'next' method that the operation classes call it with the next class as parameter.
Manager also has method like: error, parseResult (the last operation calls it).

I want to put the design I created on some charts, but it may take me some time due to high pressure at work

So,
what do you think?
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
It looks like you could benefit from learning how a Struts-based Controller works. Based on what you have written, this type of navigation is built into the Controller framework including declarative meta-data and various Action classes.

--------------------------------------------------------------------------------

Package org.apache.struts.action

The action package is the core of the Struts framework, providing the "Controller" aspect of a MVC model.

http://struts.apache.org/1.x/apidocs/org/apache/struts/action/package-summary.html
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't really see the relationship between a Struts 1 action and the CoR pattern; could you elaborate?
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
The org.apache.struts.action "package" contains the Struts implementation of the Controller functionality.

The Action class is a part of the org.apache.struts.action "package". There are many more classes however. The entire "package" makes up the open-source portion of Struts. Developers then create their customized portion for their application.

Don't really know what "CoR" pattern means however?


NOTE, there is a difference between the "action" package and the "Action" class.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Jimmy Clark wrote:The Action class is a part of the org.apache.struts.action "package". There are many more classes however. The entire "package" makes up the open-source portion of Struts. Developers then create their customized portion for their application.

I'm actually fairly cognizant of how Struts works (see signature).
Don't really know what "CoR" pattern means however?

Chain of Responsibility, which is one of the main topics of this thread.
 
GeeCON Prague 2014
 
subject: What Design Pattern should I use?