最近在学andengine引擎,想继承GenericPool<T>抽象类来实现精灵池SpritePool。需要给SpritePool构造函数几个参数,比如TextureRegion用来在父类的抽象方法onAllocatePoolItem()中创建精灵Sprite,可是,构造函数必须先调用父类的构造函数,而父类的构造函数会调用这个抽象方法去创建Sprite,而此时TextureRegion对象都还没有赋值,请问高手们该怎么样去实现这个SpritePool精灵池,在此谢谢了
下面是GenericPool<T>源码
package org.anddev.andengine.util.pool; import java.util.Collections; import java.util.Stack; import org.anddev.andengine.util.Debug; /** * @author Valentin Milea * @author Nicolas Gramlich * * @since 22:19:55 - 31.08.2010 * @param <T> */ public abstract class GenericPool<T> { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final Stack<T> mAvailableItems = new Stack<T>(); private int mUnrecycledCount; private final int mGrowth; // =========================================================== // Constructors // =========================================================== public GenericPool() { this(0); } public GenericPool(final int pInitialSize) { this(pInitialSize, 1); } public GenericPool(final int pInitialSize, final int pGrowth) { if(pGrowth < 0) { throw new IllegalArgumentException("pGrowth must be at least 0!"); } this.mGrowth = pGrowth; if(pInitialSize > 0) { this.batchAllocatePoolItems(pInitialSize); } } // =========================================================== // Getter & Setter // =========================================================== public synchronized int getUnrecycledCount() { return this.mUnrecycledCount; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== protected abstract T onAllocatePoolItem(); // =========================================================== // Methods // =========================================================== protected void onHandleRecycleItem(final T pItem) { } protected T onHandleAllocatePoolItem() { return this.onAllocatePoolItem(); } protected void onHandleObtainItem(final T pItem) { } public synchronized void batchAllocatePoolItems(final int pCount) { final Stack<T> availableItems = this.mAvailableItems; for(int i = pCount - 1; i >= 0; i--) { availableItems.push(this.onHandleAllocatePoolItem()); } } public synchronized T obtainPoolItem() { final T item; if(this.mAvailableItems.size() > 0) { item = this.mAvailableItems.pop(); } else { if(this.mGrowth == 1) { item = this.onHandleAllocatePoolItem(); } else { this.batchAllocatePoolItems(this.mGrowth); item = this.mAvailableItems.pop(); } Debug.i(this.getClass().getName() + "<" + item.getClass().getSimpleName() +"> was exhausted, with " + this.mUnrecycledCount + " item not yet recycled. Allocated " + this.mGrowth + " more."); } this.onHandleObtainItem(item); this.mUnrecycledCount++; return item; } public synchronized void recyclePoolItem(final T pItem) { if(pItem == null) { throw new IllegalArgumentException("Cannot recycle null item!"); } this.onHandleRecycleItem(pItem); this.mAvailableItems.push(pItem); this.mUnrecycledCount--; if(this.mUnrecycledCount < 0) { Debug.e("More items recycled than obtained!"); } } public synchronized void shufflePoolItems() { Collections.shuffle(this.mAvailableItems); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }
下面是我目前写的SpritePool类
package com.example.SpriteTest; import org.anddev.andengine.entity.sprite.Sprite; import org.anddev.andengine.opengl.texture.region.TextureRegion; import org.anddev.andengine.util.pool.GenericPool; /** * Created by Administrator on 2014/12/29. */ public class SpritePool extends GenericPool<Sprite>{ TextureRegion textureRegion; public SpritePool(TextureRegion textureRegion) { super(); this.textureRegion=textureRegion; } @Override protected Sprite onAllocatePoolItem() { return new Sprite(0,0,textureRegion); } public synchronized Sprite obtainPoolItem(float pX,float pY) { Sprite sprite=super.obtainPoolItem(); sprite.setPosition(pX,pY); sprite.setVisible(true); sprite.setIgnoreUpdate(false); return sprite; } @Override public synchronized void recyclePoolItem(Sprite pItem) { pItem.setVisible(false); pItem.setIgnoreUpdate(true); super.recyclePoolItem(pItem); } }
使用static修饰需要的变量,在构造函数调用之前给变量赋值