• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

mapping a string to a method call

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

currently I' looking to clean up legacy code from a former colleague and I'm facing the following problem:

I recieve an HttpRequest containing an "action" parameter. Depending on the value of "action" the controller should call the right method that will in turn produce a .jsp returned as response.

Currently this is implemented in a doAction(String action) method that is just a huge sequence of



Is there not a better cleaner, more flexible way to do this?
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can use some naming conventions, or use an enum to hold all the possible values of the action variable.
 
jean-gobert de coster
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a naming convention already, action names are in the following form
<action_type><object>

action types can be "search", "open", "sort", ... and object corresponds to an object in the domain.

so actually the doStuff() method is more like MyObjectManager.doStuff()
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the Chain of Responsibility pattern.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there not a better cleaner, more flexible way to do this?



Sure there is, just use the Struts framework. This will enable you to focus on the business requirements and leave the "plumbing" code to the experts.
 
jean-gobert de coster
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Clark wrote:

Is there not a better cleaner, more flexible way to do this?



Sure there is, just use the Struts framework. This will enable you to focus on the business requirements and leave the "plumbing" code to the experts.



Yeah that would be nice, but I'm just stuck with that legacy code. My employer doesn't want me to spend time re-doing things and he doesn't care that taking those extra days re-doing stuff will mean losing less days in the future.

I guess we all know this paradox, so for now I try to spend time seamlessly improving small parts of the code (a HttpServlet implementing 3 different makeQuery(<insert endless list of parameters here>) methods... yuck!!! ).
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can put the action name and method to be executed into a Map.

So you can have


where actions is a HashMap that contains your action name as keys and the value will be a method name to be executed.
execute - this is the method that delegates the execution. But here you will need reflection and it gets really complicated if the methods return different types of objects and take different parameters.

James said you should use Struts. I say the same or Spring MVC or one of the many frameworks.
Or if you don't want to introduce them then download the sources of either Struts or Spring and take a look at the DispatchAction, LookupDispatchAction in Struts and MultiActionController implementations in Spring. These should give you an idea on how to implement in a good way.

 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are really not going to get rid of the IF statements. You might move them to a better location, possibly use a switch statment, but that's about it.

Conditional processing, sequential processing, iterative processing are the only things you code.

My suggestion would be to leave it alone unless there are real problems (which you haven't described). If it works fine the way it is, any coding or design changes that you do or are planning to do are uneccessary.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Clark wrote:You are really not going to get rid of the IF statements. You might move them to a better location, possibly use a switch statment, but that's about it.



As far as I can tell, the post just above yours does, in fact, show a solution that gets rid of the if statements...
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The conditional processing would then shift into the HashMap implementation, where the IF statement logic is applied to process the decision of which key to select and which value to return.

The IF statement logic has not left the code. It just moved to a place that already has been coded, e.g. HashMap implementation. This is one of the benefits of using the Java Collections framework. Java Collections contain a ton of code that has already been written and tested.
 
jean-gobert de coster
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for all these answers. I like the subtelty of execute delegation (got tons to learn yet about design patterns). I do have the Spring Framework available for use. I guess there are lots of interesting things I could use from there. Initially I wanted to do an MVC design but my boss told me not to spend time doing that. But still I guess I can use some utility classes. The whole difficulty is to find quickly which things in the framework can solve my problems.

Thanks guys
 
reply
    Bookmark Topic Watch Topic
  • New Topic