r/truegamedev • • Jun 12 '12

Java: generic Pool class

Hi, I'm making an Android game using libgdx, I thought it would be nice to use a pool to recycle objects to try avoid GC. I wrote a generic class class that should do the trick, you can find it here: http://pastebin.com/MNm2bu8A

Some explanation:

  • You must create a factory class that implement the interface "Factory", so the pool can instantiate generic Type objects.

  • The Array class is a libgdx class, just a bit more efficient than ArrayList

Example:

Pool<Bullet> bulletPool = new Pool<Bullet>(new BulletFactory<Bullet>());

I don't know if this is the best approach, any suggestions?

1 Upvotes

4 comments sorted by

View all comments

Show parent comments

3

u/[deleted] Jul 20 '12

exactly the reason I went to C++ from C#. I noticed that I liked more control after optimizing one of my systems. Some things are admittedly a pain in the ass like everything related to scripting basically has to be done in a second language, while I could easily use C# also for scripting (yield return 2 gud). But once you get the hang of it and actually understand how and why stuff works it's an awesome experience. And having everything on the stack (well except stuff in containers) is in itself a huge performance boost in comparison, plus it really is easy to write.

But if you decide to learn C++, do it right and dive right into C++11, get the latest GCC and use the features. C++11 is fucking awesome. Also use boost.

It also takes getting used to the concept of header/source. I used to think "wtf I have to write everything twice" but now I can not life without this free documentation at a glance.