I already posted this on Stack Overflow, but someone suggested to post it to Code Review instead.
I am creating an Android app that draws to the canvas
. For this, I have defined rectangular screenareas that I want to draw to the canvas. As I am always drawing a fixed set of screenareas, I was thinking of using Enum
(as Enum
is designed for fixed sets).
Here is my enum
:
public enum LayoutEnum {
FULLSCREEN(
new ScreenArea(
new Rect(
0,
0,
MainActivity.getDevice().getWidth(),
MainActivity.getDevice().getHeight()),
Attributes.BG_PAINT)),
LOGO_AREA(
new ScreenArea (
new Rect(
(int) (0.3 * FULLSCREEN.getScreenArea().getArea().width()),
(int) (0.3 * FULLSCREEN.getScreenArea().getArea().width()),
(int) (FULLSCREEN.getScreenArea().getArea().width() - 0.3 * FULLSCREEN.getScreenArea().getArea().width()),
(int) (0.7 * FULLSCREEN.getScreenArea().getArea().width())),
Attributes.BG_PAINT)
);
private ScreenArea screenArea;
LayoutEnum(ScreenArea screenArea) {
this.screenArea = screenArea;
}
public ScreenArea getScreenArea() {
return screenArea;
}
}
ScreenArea
is a simple class that holds a Rect
and a Paint
and contains a draw
method (and some getters and setters).
The question I have, is: is this a good approach? On one hand I am working with a fixed set of variables. On the other hand, these variables are mutable and I can change their attributes (e.g., using the getters and setters). For example, I can call FULLSCREEN.getScreenArea().getPaint().setColor(Color.BLUE)
When you look at Enum
it says it is
a special data type that enables for a variable to be a set of predefined constant
So I do have a fixed set, it is predefined, but not necessarily constant.
My original approach was to define a class called Layout
which contained a HashMap
of Screenarea
's. In that case, I was using e.g., Layout.get("fullscreen").draw(canvas)
to draw the screenarea to the canvas. In this new approach I am using e.g., FULLSCREEN.getScreenArea().draw(canvas)
.
One of the reasons I would like to switch is to introduce a typesafe solution. Of course, it would also be possible to switch from a HashMap
to an EnumMap
and store the names of my screenareas in an Enum
.
Hope you can point me in the right direction: a direction that not only works (the above is already working) but is also acceptable and doesn't smell.