aspose file tools*
The moose likes Java in General and the fly likes exchange if-elseif trees to ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "exchange if-elseif trees to ..." Watch "exchange if-elseif trees to ..." New topic
Author

exchange if-elseif trees to ...

Mike Nightsky
Ranch Hand

Joined: Aug 18, 2001
Posts: 48
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


Win the opportunity to make money on the Internet<br /><a href="http://sweeps.sitesell.com/minirich.html" target="_blank" rel="nofollow">http://sweeps.sitesell.com/minirich.html</a>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
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

Joined: Jan 10, 2002
Posts: 61657
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike Nightsky
Ranch Hand

Joined: Aug 18, 2001
Posts: 48
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

Joined: Nov 08, 2007
Posts: 457
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.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

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

Joined: Dec 20, 2006
Posts: 89
Chain-of-responsibility?

http://commons.apache.org/chain/


Author of <a href="http://www.newsinjector.com" target="_blank" rel="nofollow">NewsInjector</a>
Mike Nightsky
Ranch Hand

Joined: Aug 18, 2001
Posts: 48
@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

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Jul 26, 2007
Posts: 10523
    
    9

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?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Originally posted by Maneesh Godbole:
This is the Ernest Friedman-Hill from the ranch?
yes.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mike Nightsky
Ranch Hand

Joined: Aug 18, 2001
Posts: 48
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: exchange if-elseif trees to ...