The Façade

Listen to this post
Subscribe

Intent

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. (GoF)

Example

When planning a vacation, business trip, or other travel there are a lot of different aspects that have to be reserved, scheduled, and otherwise arranged for. Each of these elements (a hotel, a rental car, etc.) has a different way of accessing it. In the past, this would involve lots of phone calls, the use of a legacy system called Saber, some faxes… A lot of people preferred to use a travel agent to handle all of these details.

Conceptually, a travel agent is a Façade: one person to deal with who hides all the complexity of the various interactions involved. They may even do things that customers do not know are needed. Today there are web-based services that do the same, and they are also an example of the Façade pattern. They present a single interface that encapsulates all of the varying interfaces involved.

Diagram

Facade Pattern

Qualities and principles

The Façade decouples all clients from the various interfaces it encapsulates, and how those types are designed and created. The interface of the Façade is not based on any service implementation, but on the needs of the clients that use it. Façade separates the concerns of use from all other concerns.

Testing

The Façade itself can be mocked, making the testing of all clients fundamentally simpler and faster.

Questions and concerns

Facades can end up being fairly heavyweight, in terms of their memory footprint and instantiation time, so it is usually best if multiple instances of the Façade are not needed. This means the Façade should not become specialized for a particular client, and thus should contain no client-specific state. A Flyweight can be used to prevent this and making the Façade a Singleton can enforce that there is only one Façade created. Once the Façade interface is created, Adapters can be used for varying client needs.

Facades are often used when developing new code that has dependencies on legacy code. If a Façade is introduced between new development and legacy, then the legacy nature not influence new work. Façade makes legacy refactoring less risky.

More information

Visit the Net Objectives Pattern Repository: Facade Pattern

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.