CellDrawableProvider
is an Android class used in the game Tic-Tac-Toe. It provides drawables for cells of game board (drawables for figures X and O, and "Fire" if the cell is in the firing line). To save memory, this class uses a map
to store the already created drawables.
class CellDrawableProvider {
private final Map<Object, Drawable> drawables;
private final Resources resources;
CellDrawableProvider(Resources resources) {
this.resources = resources;
this.drawables = new HashMap<Object, Drawable>();
}
Drawable getCellDrawable(int moveIconId, int fireIconId) {
if (fireIconId == android.R.color.transparent) {
return getDrawable(moveIconId);
} else {
return getLayerDrawable(moveIconId, fireIconId);
}
}
private Drawable getLayerDrawable(int moveIconId, int fireIconId) {
Object layerDrawableKey = Pair.create(moveIconId, fireIconId);
Drawable layerDrawable = drawables.get(layerDrawableKey);
if (layerDrawable == null) {
layerDrawable = createLayerDrawable(moveIconId, fireIconId);
drawables.put(layerDrawableKey, layerDrawable);
}
return layerDrawable;
}
private Drawable createLayerDrawable(int moveIconId, int fireIconId) {
Drawable[] layers = new Drawable[2];
layers[0] = getDrawable(moveIconId);
layers[1] = getDrawable(fireIconId);
return new LayerDrawable(layers);
}
private Drawable getDrawable(int iconId) {
Drawable drawable = drawables.get(iconId);
if (drawable == null) {
drawable = resources.getDrawable(iconId);
drawables.put(iconId, drawable);
}
return drawable;
}
}
I do not like the class name CellDrawableProvider
and method name getCellDrawable()
.
Can you suggest better names?