Amir Ghahrai
Amir Ghahrai

Reputation: 619

How to avoid a large if-else statement in Java

I'm developing a framework in java which relies on a number of XML files with large number of parameters.

When reading the parameters from the XML file, I have to have a large if-else statement to decide what the parameters is and then call appropriate methods. Is this normal? to have a large if-else statement?

I am thinking that there is a simple and neater way of doing this, e.g. Java XML mapping or Java Reflections? is this the answer? if so, can you please provide examples of how this is done so I don't have to rely on a large if-else statement?

Thanks!

Upvotes: 1

Views: 905

Answers (7)

Thomas Jones-Low
Thomas Jones-Low

Reputation: 7161

You want to first create an interface:

public interface XMLParameterHandler {
  public handle_parameter (String XMLData);
}

Next you want to create a map:

private Map<string, XMLParameterHandler> handlers;

...and initialize it with one of the relevant Map implementations:

this.handlers = new HashMap<>();

You need to implement the interface on a number of classes, one for each parameter you intend to handle. This is a good use of inner classes. Insert each of these implemented handerls into the map:

handlers.put ("Param1", new XMLParam1HandlerImpl());
handlers.put ("Param2", new XMLParam2HandlerImpl());

Then you can call the handler from the xml processing loop:

handlers.get (paramValue).handle_parameter(XmlData);

Upvotes: 3

Larry Watanabe
Larry Watanabe

Reputation: 10184

N functions with M parameters can always be implemented with a single function with M + 1 parameters.

If you need a big if then else statement to decide which method to dispatch to, then you can just add a parameter to your method and call a single method.

You shouldn't need an if-then-else statement to bind the parameter values.

If there is complex logic dependent on the particular parameter values, you might use a table driven approach. You can map various combinations of paramemter values into equivalence classes, then variouos equivalence class combinations into a row in a table with a unique id, then have a switch statement based on that unique id.

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163262

It sounds to me as if you want a data-driven rule-based approach to writing your application, rather like you get in XSLT. One way of achieving this is to write it in XSLT instead of Java - XSLT, after all, was specifically designed for processing XML, while Java wasn't. If you can't do that, you could study how XSLT does it using rules and actions, and emulate this design in your Java code.

Upvotes: 0

Raedwald
Raedwald

Reputation: 48616

a large if-else statement to decide what the parameters is and then call appropriate methods

You could instead use the Strategy design pattern, with one Strategy object per parameter, and use a map from the parameter name to the Strategy object to use. I've found this approach useful for even a moderately complicated application of XML.

Upvotes: 0

Tom Anderson
Tom Anderson

Reputation: 47163

Reflection would be one approach. Perhaps combined with a custom annotation on the target method to indicate which parameter to pass to that method. This is an advanced technique, though.

A more standard technique would be to use a map, where the key is the attribute name, and the value is an instance of an implementation of some interface you define, like AttributeHandler. The implementations then contain the code for each attribute. This involves writing a lot of little classes, but you can do them as anonymous classes to save space and keep the code inline.

Upvotes: 0

Nirmal- thInk beYond
Nirmal- thInk beYond

Reputation: 12054

i recommend to use Map, that have parameter as key and xml entry as value(not whole xml)

Upvotes: 1

Andrey Marchenko
Andrey Marchenko

Reputation: 156

There is JAXB (http://en.wikipedia.org/wiki/Java_Architecture_for_XML_Binding) for mapping java class to xml.

But you can't map methods with it: you only can map attributes to xml file values (deserialize parameters from xml).

Upvotes: 2

Related Questions