Originally Posted by
danielstoner
Hi LVH,
I am not sure how advanced a programmer you are because your profile doesn't mention it. I am gonna describe here a solution and if you need more guidance let me know.
Java has a very nice feature called "Reflection". For your problem this is the perfect solution. what you have to do is to create a factory class that is able to read your configuration file and based on what you have in there it will create instances of your objects.
In order to be able to do this you have to make your classes obey to some standards. First one you already have, they are all derived from a parent class or implement the same interface. This helps with the design of your factory class.
The second requirement is for your classes to have a constructor with the same signature. To keep it simple for now you can make your classes to have a no parameters constructor.
Then you have to modify your configuration file to specify the name of each class as "package.Table" or something similar.
Now when you start your program you create the factory instance with the configuration file as parameter. While you read the class names from the file you can instantiate them using reflection with the call java.lang.Class.newInstance(). Here you have to deal with a number of exceptions.
If this is not clear enough I can write a small example for you when I have 20 minutes. But I would like to see you try it first. Post the code here. Cheers.
Java 101, but extensive Perl5Moose and Perl6 OO experience (and notions of Ruby/Python newstyle classes).
Right. I had found reflection myself thanks to the help of the good people at #java @ Freenode and I've gotten newInstance to work by myself.
As an exercise, I'd now like to use .getConstructor too -- but I can't get it to work

(pastebin inc at end of post). I can't figure out how getConstructor works. It's a method that applies to Class objects, but somehow it expects a class parameter?! (The error I get when I try to compile is (".class expected") See the pastebin for more info on how I managed to mess it up
newInstance() doesn't work because I don't have argless constructors -- but it does throw the exception I expect it to throw -- that's a good thing I suppose
The constructors I have are of the form public SomeClass (int[] args) -- this is good because int[] args is what I get from my parser ;-)
http://pastebin.com/m6495af8c
Of course, I could just write empty constructors, use super(), and make methods in my superclasses/subclasses (depending on the amount of classes that have that particular attribute) but that is suboptimal because Chairs (x,y,radius) have less arguments than Tables (x,y, width, height) -- would be cool if I could just pass an int[] to the cnstructor and then handle it there.
We haven't seen reflection in class, but from what I've read I understand its equivalent form in other OO languages.