Design Patterns – How to Implement Graceful Fallback on Old Platforms

cross platformdesign-patterns

Let's say I need to add a drop shadow behind a box. Some old platforms do not support drop shadows, so I have to fake it by putting an image behind the box. Here's the pseudo code of how I'm currently handling this fallback:

if (dropShadowsAreSupported) {
    box.addDropShadow("black");
} else {
    box.putImageBehindIt("gaussianBlur.png");
}

Is this the right way to handle it? It seems too amateur to me. Is there a better design pattern?

In my actual project, there are a ton of places where I need to style the user interface differently for different operating system versions. It's not only one if-else.

Best Answer

if you are going to do something like that then conditional compilation or different library implementations are the key

this way you can say

addShadow(box,black);

and depending on what library you link it with it will become either the addDropShadow or the addImageBehind

in C you could also say

#IFDEF DROP_SHADOW_SUPPORTED
    box.addDropShadow("black");
#ELSE
    box.putImageBehindIt("gaussianBlur.png");
#ENDIF

you abstract this (or it is already abstracted) in your graphics library so the source code won't be need to change only the library

Related Topic