just for having practice with design pattern and clean code I'm coding a sample java application that can update an Icon. The following switch case does not really satisfy me. I have the feeling this could be refactored to something better. Maybe you have an idea for refactoring, or maybe you say: 'everything is just fine.'? :-)
Some words concerning the code:
there is a public method where you can update an Icon by giving a state and a text, like weather state is COLD and text is 20 (degree) for example. createBufferedImage is doing some switch case on the state, so it can let drawIconImage(test, fontcolor, backgroundcolor) (not visibile here) draw an image, return it, so setIconImage can set it.
private void setIconImage(Image image) {
trayIcon.setImage(image);
}
public void updateIcon(State state, String text){
setIconImage(createBufferedImage(state, text));
}
private BufferedImage createBufferedImage(State state, String text) {
if (state == null || text == null) {
return drawIconImage("Error", Color.WHITE, Color.RED);
}
switch (state) {
case HIGH:
return drawIconImage(text, Color.RED, Color.WHITE);
case MEDIUM:
return drawIconImage(text, Color.ORANGE, Color.BLACK);
case LOW:
return drawIconImage(text, Color.GREEN, Color.BLACK);
case ERROR:
return drawIconImage("Error", Color.WHITE, Color.RED);
default:
return drawIconImage("Error", Color.WHITE, Color.RED);
}
}
I would be happy if you have some ideas or words for refactoring, concerning clean code and using design pattern.
thx u