Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Else if..

 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
I just needed to come up with an alternate to creating a conditional that ends up having 20 else if statements.

If it helps, I am just receiving an object and looking at a particular property and then redirecting to a method based on the value of that property.

Any ideas on a better, cleaner way to be doing this?
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you not use a Select Case statment, it may be called something else in Java.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's called "switch" in Java, and that's definitely an option.
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have this type of thing, consider whether polymorphism and overriding is a better solution.

But there is a place for the switch statement. However, switch only works on certain types (byte, short, int, char, and enum).

If neither polymorphism or switch work for your domain, then the if-else-if-else-if... is what's left and it has its place too.

Kaydell
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The other main option is to use a Map (usually a HashMap) to take a String or other object type and use it to look up some value. There are different ways to do this, depending on what you're trying to do within each if block.
 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was also hoping to avoid a switch statement that had 20 or so cases.

With a hashmap, can I look up a value, and have that value be associated with a method?

So search for a value, find it, and then call the associated method?


I guess I am just looking for a more elegant, or cleaner way of making code when dealing with a huge conditional statement, whether it be switches, or else ifs...

Can I do what I am trying to?

Does it make sense?
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand exactly what you're trying to do.

Can you have one base class with 20 subclasses and have an object reference variable of the base class and call its method. Then the right things would just happen automatically, but I'm not sure if this would work for what you're doing.

Kaydell
 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does sound a little better...

Is there something I can read on doing that sort of thing, I am not quite sure how to start doing that...
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The keyword to search for is "polymorphism".

You can try my "Java Search" below and search "Java Books" for "polymorphism".

Kaydell

Java Search
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a hashmap, can I look up a value, and have that value be associated with a method?


Absolutely! What if you moved the code you want to run for each choice into the objects in the map?

Strategy is the name of one of the Gang of Four "design patterns", a common solution to a common problem. Can you imagine how the map could be full of strategies that know how to handle different scenarios? I made my strategies very simple here. You might have to pass in some parameters or call a couple methods on one if your problem is more complex.
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan James
The other main option is to use a Map (usually a HashMap) to take a String or other object type and use it to look up some value.


That's an interesting thought. I'll have to try Map objects.

Kaydell
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am talking WAYYYYY out of my league here..... I am only about halfway through with HFJ on my way towards my SCJP. BUT.... I have a 20+ year background in programming. What I am getting at, I don't know if this can be done in Java or not, but I thought I would throw the concept out there and let it get supported... or shot down.

From my C background I know that you can create arrays of pointers to functions and use an integer as a "function selector" or "handler selector" and simply reference the array using the "selector integer" to index into the array, pass any arguments in parens, and kaBAM you are gone in a couple of lines of code.

What I was thinking of here is an enum of values to serve as "handler selectors" and perhaps create a class Handler that had a reference to a method() as a variable, a constructor that allowed you to create a handler object, passing the method name to it, and in the class have a common method name that calls the stored method.
You could then declare an array of Handler objects and initialize it with the number of handlers you wish to define, passing each constructor the name of the handler for that enumerated value.

Now, you probably all think I'm out of my head over here because frankly I don't know if you could pull this off in Java or not. I have heard of something called delegates that allows the registration of methods() for later event handling, so I thought that there might be a way to use something like that to populate an array. If you could, talk about quick method dispatching......pass an enum into the main handler method and that enum is used as an index into an array of objects, passing any args required, and boom she was gone!

Well...... now that I have throughly embarrassed myself, I'm going to sit back and see what happens.....

Bob
 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan,
I really like what your code is looking like, I am trying to look some things up on this, and still looking. I hope to be there soon...

Bob,
I dont have nearly as much experience as you do (only about 2 years), but I was wondering how do you go about creating those pointers?
 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things I came across are reflection, and interface.

The interface (I think) has me put in an object into the hashmap that implements the interface.

The other thing that I was was reflection, which looks like I put the method name into the hashmap, and then look for in in some metadata(not sure where it comes from)

Both of these seem out of my league. I cant seem to find much information about these two techniques that I can understand, can anyone help dumb it down for me a little bit?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The interface solution seems interesting. See if you can follow what going on here:



[ July 13, 2007: Message edited by: Garrett Rowe ]
[ July 13, 2007: Message edited by: Garrett Rowe ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob, we don't have pointers to functions in Java. Do you find the Strategies Garrett sketched out solve similar problems?

Ryan, we can get more abstract with reflection for method names. I'm feeling I'd rather not go there in the beginner forum, though. Too many choices, maybe.

I have a bunch of situations where I get all this from configuration. An application assembler reads config, sets up the map and hands it to the component that needs it.

Yes, that is dipping our toes in reflection, just not going over our heads.
 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok great, thanks a lot. I am going to take that code sample and see if I can come up with something.

Be back soon
 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Worked wonderfully, thanks for all of the help and code examples
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic