Re: How to make a class an alias of another one?
George wrote:
Thank you all very much. I am a bit overwhelmed to be frank. In your
solution, I have to getInstance everytime, don't I?
Well, every time you need a new instance, yes.
Here is more detailed situation. The object is an enumerated
quantity. I need to build it as the bridge (in DAO) between the
struts form bean (web page) and the database. If I implement it as a
I have to confess you lost me here, I think you've passed what I know
about. Maybe someone more experienced with this style of programming can
give you some ideas.
enum, I need to write quite some extra methods to convert the String
input from form bean to the enum and back to the String for the
database. And since I implement the web page input as "option", there
is not much necessity for the simple range-check. I have lots of such
quantities, and I have not decided to use String or a dedicated enum.
Don't forget that you can use interfaces with enums. If you have an
enum with choices (let's call them "Choice1", "Choice2" and "Choice3")
you can say that ClassA is OptionChoice (ClassA is your base class from
your first post) and do something like this:
enum MyChoices implements OptionChoice { Choice1, Choice2,
Choice3 }
Now you can make your methods take "OptionChoice" instead of just the
enum. That's easier to extend later.
Plus I do not have enough time now. So I feel I might use String now
and later use a more proper enum. Furthermore, with a enum vs.
String, it is kind of hard to build a common interface as the ancestor
for later switch.
See above, that's how you build a common ancestor for later switch.
So you could start with something like this for now:
class Temporary implements OptionChoice {
private String [] choices; // just use strings for now...
// ...
}
And then later switch to an enum like I showed above. Note that the
class Temporary and enum MyChoices are still package private.
You'll need to think about which methods you'll need in the interface
OptionChoice. You mentioned switching between strings and some internal
type. enum has valueOf() and values() to do this, but you should make
new methods you can implement in OptionChoices.
public interface OptionChoice {
String [] getAllChoices();
Set<OptionChoice> getOptionSet( String [] options );
}
Or something like that. One method to get a list of strings, because I
think you'll need that no matter what. (HTML very text oriented). And
one method to go from a list of strings back to some sort of Set that
represents the choices the user made.
For really clean implementation, you'll need that third class I
mentioned I was trying to avoid. Can't uses static methods in an interface.
public class OptionChoiceService {
private OptionChoiceService() {} // can't call new, class final
public static OptionChoice getInstance() {
return new Temporary(); // switch to enums later
}
}
Because you'll want one point to change in the code later on, do this.
If you call "new Temporary()" yourself everywhere, you'll have a big
mess to clean up. The OptionChoiceService.getInstance() method makes it
easy to change what your type is later on, since you only have to change
one line of code.
However, the more I think about this... if your "choices" are read from
a database (and I think perhaps they should be) I don't think you'll be
able to use enums. Hard coding stuff like that is bad. You'll have to
implement your own "enum" based on String. Could be kinda tough,
there's some tricky corner cases involved. Good luck.