Creating objects based on a String value
Short problem sketch: I have a configuration file I'm parsing. This config file describes an object per line of text. I can read from the config file without problem.
Each line should result in a new object. All of these objects have a common (abstract) superclass, Furnishing. There are a number of subclasses describing the stuff I see in my living room: Couch, TV, Chair, Table...
Right now, I have something that _works_ (more or less) but is simply horribly poor code.
I've pastebinned some of this code. (Please note, I know this is horribly ugly and bad -- that's why I'm complaining):
Nopaste - No description
- (worst) This method needs to know the gory details of my object model. If I change something there, I have to change it here. Right now it makes two classes -- but I could easily end up having ten, or so. That's a lot of stupid, repetitve code. (see also 3). This means horribly poor coupling b
etween my IO class and my internal objects.
- (ugly) I have to instantiate the Furnishing F (second line) with a dummy Aquarium object that does nothing, because else the code won't compile (because there is no guarantee that f will be instantiated by the time I return it). Should be easy to fix, I guess.
- (minor, I think) The limiting factor to refactoring all of this into one method IMO is the fact t that different classes need a different amount of arguments. I could fix this by simply making a constructor that accepts "int argumemnts" instead of each separate int.
Essentially, I need something that makes objects of a class of which the name is in a String object. Whats the Right Way(tm) to do this?
Thanks in advance.