Facade
12 Mar 2022Intent
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.
Applicability
Use the facade pattern when:
- you want to provide a simple interface to a complex susbsystem.
- there are many dependencies between clients and the implementation classes of an abstraction.
- you want to layer your susbsystems. Use a facade to define an entry point to each subsystem level.
Structure
Participants
- Facade: knows which susbsystem classes are responsible for a request, delegates client requests to appropriate subsystem objetcs.
- Sussystem classes: implement subsystem functionality, handle work assigned by the Facade objetc, have no knowledge of the facade, i.e. they keep no references to it.
Collaborations
- Clients communicate with the subsystem by sending requests to Facade, which forwards them to appropriate subsystem helper.
- Clients that use the facade don't have to access it's subsystem objects directly.
Consequences
The facade pattern:
- shields clients from subsystem components, thereby reducing the number of objects that clients deal with and making subsystem easier to use.
- promotoes weak coupling between subsystem and it's clients.
- doesn't prevent applications from using subsystem classes if they need to.
Sample Code
Related Patterns
- Abstract Factory can be used with Facade to provide an interface for creating subsystem objects.
- Mediator is similar to facade in that it abstracts functionality of existing classes.
- Usually only one facade object is required, thus they are often Singleton.