Created
June 29, 2017 01:03
-
-
Save hayukleung/e4b02bc91e7e2884990c79161f5fc4f5 to your computer and use it in GitHub Desktop.
基于 SparseArray 的资源池
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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