- 1. Goals
- 2. Problems
- 3. Discussion
- 4. Structure
- 5. Example
- 6. Control List
- 7. Rules of thumb
Goals
- Provide a unified interface for a set of interfaces in a subsystem. A facade defines a higher-level interface that makes the subsystem easier to use.
- Wrap a complex subsystem with a simpler interface.
Problems
The client access segment requires a simplified interface to access the common functionality of a complex subsystem.
Discussion
A facade encapsulates a complex subsystem within a single interface. This reduces the learning curve needed to successfully use the subsystem. It also helps decouple the subsystem from potentially many clients. On the other hand, if the Facade is the only access point for a subsystem, this will limit the power and flexibility that "advanced users" may want.
The Facade object should be a fairly simple intermediary. It should not become an omniscient oracle or a "divine" entity.
Structure
The façade takes "an enigma wrapped in an enigma shrouded in mystery" and adds a shell that tames an amorphous and incomprehensible mass of software to provide a simple and understandable client interface.
SubsystemOne and SubsystemThree do not interact with SubsystemTwo internals. They use a SubsystemTwoWrapper "facade" (i.e. a higher level abstraction).
Example
A facade defines a higher-level unified interface to a subsystem that makes it easier to use. Consumers face a facade when ordering from a catalog. A consumer calls one number and talks to a customer service representative. The customer service representative acts as a front, providing an interface to the order fulfillment, billing, and shipping departments.
Control List
- Define a simpler unified interface for a subsystem or component.
- Create a 'wrapper' class that encapsulates the subsystem.
- Facade/wrapper eliminates the complexity and interaction with the component by delegating the appropriate methods.
- The client uses (attached to) only the facade.
- Consider whether the facade will add additional value and functionality.
Rules of thumb
- Facade defines a new interface, while Adapter uses the old interface. Remember that an adapter supports two existing interfaces, not defines a completely new one.
- While Flyweight shows how to make many small objects, Facade shows how to make one object representing an entire subsystem.
- A mediator is similar to a Facade in that it abstracts the functionality of existing classes. The mediator abstracts/centralizes arbitrary relationships between peer objects. In contrast, Facade defines a simpler interface for a subsystem, it does not add new functionality, and it is not known to subsystems.
- Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
- Facade objects are often singletons because only one Facade object is required.
- Adapter and facade - both wrappers; but they are different kinds of wrappers. The purpose of a Facade is to create a simpler interface, and the purpose of an adapter is to develop an existing interface. While a Facade typically wraps multiple objects, an Adapter wraps a single object; The facade could have an interface complex with a single complex object, and the adapter could wrap multiple legacy objects.