I'm coding a little casual game in Java using Slick2D.
This game use a lot of different "objects", managed in a composite way. So, firearms, furniture in the map, NPC and player character will be composites objects using components to define their behaviors.
Since I'm going to need a lot of, for instance, "shotgun" object, I'm using a sort of prototype pattern. When the player starts the game, XML files are read, and from those XML, typical objects are built and stored in a "EntityPool" class' hashtable. Then, when I'll need a shotgun, for instance, I'll ask for it to the EntityPool, which will give me a clone of this object.
My problem is as follow : I'm using JDOM to parse my XML. I don't know how many components I'm going to use, nor their exact methods. So I'd like to use reflection; a
Class.forName(nameOfMyClass) in order to get the exact component for this class, and then a
Class.getMethod("set" + propertyName) to fill the information I need.
So my XML can be like that :
<Entity name="window"> <Component class="Breakable"> <health>12</health> <newEntityAfterBeingBroken>brokenWindow<newEntityAfterBeingBroken> </Component> <Component class="Renderer"> <tileset>object.png</tileset> <imgnum>2</imgnum> </Component> </Entity> ...
There are obvious drawbacks to using reflection, particularly the lack in performance. But this is done only once, when loading the game. Do you see other problems with this method? Would you do it differently, and how? Thanks in advance for your advice!