1

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.

3
  • 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, 2012 at 21:16
  • Is this question in relation to a specific programming language? May 31, 2012 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, 2012 at 23:57

3 Answers 3

3

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

2
  • I like this one. Is it called the Encapsulate-into-a-function pattern? :p
    – JoJo
    Jun 1, 2012 at 0:02
  • 1
    eh no I think it's called an abstraction layer Jun 1, 2012 at 0:31
7

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

3
  • 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, 2012 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. Jun 1, 2012 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, 2012 at 7:21
2

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.

3
  • 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, 2012 at 0:04
  • +1 for the last line: Pity more people don't abide by it
    – mattnz
    Jun 1, 2012 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, 2012 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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