Search code examples
javaandroidlibgdx

Better ways to handle object creation?


I'm working on a game which has lots of types of objects, all of them extend GameObject class. Every object has it's own constructor. My Main class has createGameObject() method which accepts Class<?> type and it compares with each one of the objects classes in the game. I have to do that because each type has different constructor.

My question:

Is there a better way to handle this creation? Making it more generic? My game has lots of types and it makes the code huge because of the comparisons.

I'm making my game in Java, for Android, using LibGDX


Solution

  • I think the Factory design pattern would come in handy for you in this case. :)

    The whole point is that Object creation happens in a completely separate class, making everything easier to maintain.

    You can get a basic overview of this design pattern here. Enjoy!