This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exchange if-elseif trees to ...

 
Mike Nightsky
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im writing an application which can be configured with defined confiugrations (baseline) at the moment 5-8.
So some functions (for gui, check algorithms) have to do the processing in diffent way depending on the baseline. Not completly different but, slightly other.
Now we do this :

This is very hard to maintain...
So we want to change this to something more sofisticated.

Additional we sometimes have to handle fields for a baseline that do not exist in an earlier one.

I would appreciate any help with this topic

Thank you in advance
Mike
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the baselines into a Map as "K" and some sort of object which handles the differences as the "V"??
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, define an interface that handles all the methods that are different across configs. Instantiate an object for the current baseline that provides the appropriate functionality for that baseline. The mainline code will not need to care which it is dealing with.
 
Mike Nightsky
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanks.
@Champbell Ritchie
This would work fine for one occurence but there are about 100-150 Objects that need different handling for each and every baseline.
Different checking algorithms and additional different gui visibility.

@Bear Bibeault
Then the if-elseif tree just moved a bit, but still exists.
By the way this was how the old application was meant to be.
[ June 25, 2008: Message edited by: Mike Nightsky ]
 
Bill Shirley
Ranch Hand
Posts: 457
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends greatly on what the "special handling" for each case is.

If you could show us a portion of actual code, we could better direct your refactoring efforts.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Nightsky:
Then the if-elseif tree just moved a bit, but still exists.
By the way this was how the old application was meant to be.

Not really. I use this technique all the time with no else-if constructs. It's usually a matter of configuration and/or naming conventions.
 
Vilmantas Baranauskas
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chain-of-responsibility?

http://commons.apache.org/chain/
 
Mike Nightsky
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear Bibeault:
I have to decide which object to instantiate at runtime. Because, some times the baseline can change at runtime.

@Bill Shirley:
Sorry, i don't have an excample at hand now. But it's like checking ranges, and enum values.

@Vilmantas Baranauskas:
I will discuss this with the architects in the other teams at my company.

Thanks for your answers.
Mike Nightsky
 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The if/else if/else if chain grows into a really long switch statement, and that's not good. You want to do something OO, like just do a polymorphic 'execute()' on a suitable object

Originally posted by Vilmantas Baranauskas:
Chain-of-responsibility?


Its a possibility, altho I really dislike tools that require two languages, in this case, Java + XML
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering if Jess can be put to use here.

PS. From the website
Jess is a rule engine and scripting environment written entirely in Sun's Java language by Ernest Friedman-Hill...

This is the Ernest Friedman-Hill from the ranch?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maneesh Godbole:
This is the Ernest Friedman-Hill from the ranch?
yes.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pat Farrell:

Its a possibility, altho I really dislike tools that require two languages, in this case, Java + XML


Who says that Chain Of Responsibility needs XML?
 
Mike Nightsky
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your thoughts and answers.

Our architects have some thing thought up with the command pattern. We give it a try in a prototype.
But a solution with a rules engine (Jess, JBPM) has not died completly.

Thank's for your help and keep up the good work you all do here!

Mike Nightsky
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic