Skip to content

Instantly share code, notes, and snippets.

@hayukleung
Created June 29, 2017 01:03
Show Gist options
  • Save hayukleung/e4b02bc91e7e2884990c79161f5fc4f5 to your computer and use it in GitHub Desktop.
Save hayukleung/e4b02bc91e7e2884990c79161f5fc4f5 to your computer and use it in GitHub Desktop.
基于 SparseArray 的资源池
import android.support.v4.util.SparseArrayCompat;
/**
* 资源池
*/
public class Pool<T> {
private SparseArrayCompat<T> mPool;
private New<T> mNewInstance;
public Pool(New<T> newInstance) {
mPool = new SparseArrayCompat<>();
mNewInstance = newInstance;
}
public T get(int key) {
T res = mPool.get(key);
if (res == null) {
res = mNewInstance.get();
mPool.put(key, res);
}
return res;
}
public interface New<T> {
T get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment