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.
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.
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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
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.....
SCJP - 86% - June 11, 2009