I’m refactoring some code in project whilst reading Object-Oriented
Reengineering Patterns specifically the section on “Transforming Conditionals to Polymorphism”. So the current code base has constant that is referencing a factory class that which returns a bitmap based on displayWidth & displayHeight. To achieve this do I need to create two new classes, with each representing the differing screenWidth and screenHeight ? I’m a litte lost as to whats the best method of achieving polymorphism in this case.
public static final Bitmap TICKER_BACKGROUND_IMAGE = ImageFactory.getFooterBitmap();
Method in ImageFactory –
public class ImageFactory {
private static int displayWidth;
private static int displayHeight;
static {
displayWidth = Display.getWidth();
displayHeight = Display.getHeight();
}
public static Bitmap getFooterBitmap(){
if(displayWidth == 360 && displayHeight == 480){
return Bitmap.getBitmapResource("360x480/footer_bg.png");
}
else {
return Bitmap.getBitmapResource("320x240/footer_bg.png");
}
}
}
I would take all it arguments as parameters. Don’t use static variables as argument if you can.
Using polymorphism is a good idea but not the best solution in every situation.
Another approach might be to see if the size is available and use a fall back position.