Façade

The Façade Pattern provides a higher level, unified interface to a set of interfaces in a subsystem with the intent of making it easier to use. The second part of chapter seven of Head First Design Patterns would tell you more on this pattern, here I write a few lines on it.

As example of usage of the Façade Pattern we can think of a complex system emulating a home theater. It consists of an Amplifier connected with a Tuner, a CD Player, a DVD Player. The DVD Player has to be connected to a Projector, and we'll also have a Screen, a Theater Light Manager and even a Popcorn Popper.

A cool system, indeed. But it requires quite a while to run. Watching a movie is not an easy task. Even if we have a standard procedure we know we are about to execute each time (starting with turning on the popcorn popper, ending with actually starting the DVD Player), we have no logical place where to store it for subsequent usage.

Well, the idea of the Façade Pattern is exactly to provide such a place.

In our specific case, we would create a class HomeTheaterFacade that would make available to the client a few methods, like watchMovie(), endMovie(), listenCD(), endCD() ..., that would let us access the system in a standard way taking care of the details.

The HomeTheaterFacade is just a shortcut to the Home Theater classes. We still have access to the original classes so, for instance, we can access the DVD Player to skip over a scene we find boring.

No comments:

Post a Comment