Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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) {
} else {

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.

share|improve this question
when platform differences are significant, Bridge pattern comes to mind: "decouple an abstraction from its implementation so that the two can vary independently..." – gnat May 31 '12 at 21:16
Is this question in relation to a specific programming language? – Bryan Oakley May 31 '12 at 23:24
@BryanOakley This happens quite a lot in Objective-C when programming for different iOS versions. Apple often likes to deprecate APIs and replace them with a new APIa that are not backwards compatible. – JoJo May 31 '12 at 23:57
up vote 3 down vote accepted

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

this way you can say


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

in C you could also say


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

share|improve this answer
I like this one. Is it called the Encapsulate-into-a-function pattern? :p – JoJo Jun 1 '12 at 0:02
eh no I think it's called an abstraction layer – ratchet freak Jun 1 '12 at 0:31

This sounds like a job for the Strategy Pattern. Pass in a rendering strategy, and instantiate the rendering strategy based on your platform.

share|improve this answer
I think you need more than just Strategy Pattern. At a higher level, you need a Factory. This factory takes in a platform version. If the platform version is high enough, return the high platform strategy, else return the low platform strategy. – JoJo May 31 '12 at 23:55
@JoJo - yes, of course, but you've already figured out if such-and-such is supported, according to the question. Instead of putting those if/thens in the rendering code, you put those in the factory. – Scott Whitlock Jun 1 '12 at 0:26
+1 for strategy. And strategy implies some form of factory as well, to use it in a complete solution. – pap Jun 1 '12 at 7:21

As Scott Whitlock mentioned, a strategy can provide different implementations of the behavior. That said, it is probably over-engineering in the supplied case. Not everything has to fit into a pattern; some simple conditional behavior like this is fine as a simple conditional. No need to make things more complicated if they don't need to be.

share|improve this answer
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. – JoJo Jun 1 '12 at 0:04
+1 for the last line: Pity more people don't abide by it – mattnz Jun 1 '12 at 0:14
@JoJo: that's why CSS and WPF styles and (a bunch of other presentation layer abstractions I'm unaware of) exist. They're large enough to be on the border of being outside of pattern territory. – Telastyn Jun 1 '12 at 0:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.