Alpesh-Prajapati / andengine

Automatically exported from code.google.com/p/andengine
0 stars 0 forks source link

GenericPool constructor issue #61

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When I tried to use the initial size and growth parameters in the GenericPool I 
ran into the issue that GenericPool tries to call the overriden method 
onAllocatePoolItem() from the constructor (specifically the 
batchAllocatePoolItems method). This is typically frowned upon.

As explained in Effective Java 2nd. Ed. (Chapter 4, Item 17):

There are [...] restrictions that a class must obey to allow inheritance. 
Constructors must not invoke overridable methods, directly or indirectly. If 
you violate this rule, program failure will result. The superclass constructor 
runs before the subclass constructor, so the overriding method in the subclass 
will get invoked before the subclass constructor has run. If the overriding 
method depends on any initialization performed by the subclass constructor, the 
method will not behave as expected.

In my case my pool depends on a TextureRegion being set before anything is 
allocated because I have a pool of Sprites. 

I think I can work around this by calling batchAllocatePoolItems toward the end 
of the constructor, but I'm still not able to set the growth to something other 
than the default.

My suggestion would be to move the setting of initial size and growth to an 
Init() type method which can be called after the constructor has completed. I 
know it's not ideal, but perhaps it could be ReInit() for people who need the 
subclass to initialize before allocating any objects.

Original issue reported on code.google.com by Nicholas.Nezis on 30 Dec 2011 at 5:59