
Design Patterns - Facade Pattern
Originally published on my old blog at cumps.be on July 20, 2008.
Time for another simple design pattern: the Facade Pattern. I’m going to need a break soon. I’m getting a bit burned out, which is never a good thing.
Anyway, the definition of today’s pattern: “Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.”
The hardest part of writing articles for this series isn’t so much understanding the patterns themselves, but coming up with examples which somewhat make sense in my gaming example. So, please forgive me if they’re sometimes a bit far-fetched, like today ;)
Let’s say we have some complex subsystem which handles everything about storage. Some classes for our CD drive, some for our hard disk, memory and much more.

If we need to load a new map, one way to do it would be to load up the CD, copy it to the hard disk, load it into memory and stop the CD from spinning. Or, we could also create a new class, which defines some more high-level operations such as LoadMap(), which takes care of dealing with the subsystem.
This way, our clients can use a simple interface, but if they really need to, can still go straight to the subsystem classes themselves, as opposed to the Adapter Pattern, where you only talk to the Adapter.

And that’s it really, Facade and Adapter are very alike, and both very simple.
Let’s have a look at the full diagram, and imagine the subsystem contains a lot more classes than I drew :)

Time to test it and have our client use the StorageFacade directly, instead of the subsystem.

I’ve uploaded the solution again, worth a look.
In my opinion, the Facade Pattern is a helper pattern to make your code easier to use and more user-friendly, other developers being the user.
Some additional information on the Facade Pattern:
You May Also Like

Design Patterns - Adapter Pattern
The Adapter Pattern: making a third-party Structure behave like our own IBuilding, the way a travel plug adapter does.

Design Patterns - Command Pattern
The Command Pattern: UI buttons that run commands without knowing about game units, with undo for the last action.

Design Patterns - Generic Singleton Pattern
A generic Singleton base class that creates its instance through the private constructor with reflection, worked out with readers' help.



