The Facade Design Pattern
The Facade pattern provides a unified interface to a set of interfaces in a subsystem, simplifying access to its functionality.
Use the Facade pattern when you want to provide a simplified interface to a complex subsystem. This pattern makes the subsystem easier to understand, use, and test.
- TypeStructural
- Time ComplexityO(1) Constant Time – The algorithm runs in constant time, irrespective of the input size. Operations like accessing an array element fall into this category.
- Efficiency8
-
Learning Effort
- Average3
- Conceptual3
- Debug and Maintain4
- Implementation2
- Prerequisites2
- Versatility5
Things you should know before you start
- Encapsulation Understanding of encapsulation to hide the complexities of the subsystem.
- Composition Understanding of composition to manage multiple subsystems.
Minimal Example
Simplifying the process of turning on a computer, which involves multiple steps like booting the OS, initializing drivers, and so on.
class ComputerFacade { private computer: Computer; public turnOn() { this.computer.startBoot(); this.computer.initDrivers(); } } const computerFacade = new ComputerFacade(); computerFacade.turnOn();
What’s happening in thie example
In this example, the `ComputerFacade` class simplifies the complex `Computer` subsystem. The `clientCode` shows how the computer can be turned on with a single method call.
How the Facade Works
The Facade pattern simplifies complex systems by providing a single, unified interface that handles multiple subsystems.
This pattern is particularly useful when working with libraries or APIs that have complex setup processes or numerous methods. It shields clients from the complexities of the subsystem.
Entities in the Facade Design Pattern
-
FacadeThe Facade class provides a simple interface to the complex logic of one or more subsystems.
- Facade has a one-to-many uses relationship with Subsystem
What to watch out for when using or considering this design pattern.
Avoid using the Facade pattern when the subsystem is not actually complex, as it can add unnecessary layers of abstraction.
Design patterns often confused with the Facade Design Pattern
The following design patterns are often confused with the Facade
for various resons.
If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.
-
Adapter – The Adapter pattern allows objects with incompatible interfaces to collaborate.
The Adapter pattern is similar to the Facade in the following ways.
- Both are structural design patterns.
- Both aim to simplify interfaces.
- Facade provides a simplified interface to a complex subsystem, whereas Adapter makes two different interfaces compatible.
- Facade deals with a whole subsystem, while Adapter focuses on making individual interfaces compatible.
-
Decorator – The Decorator pattern attaches additional responsibilities to an object dynamically.
The Decorator pattern is similar to the Facade in the following ways.
- Both are structural design patterns.
- Both aim to extend functionalities.
- Facade simplifies access to a complex subsystem, while Decorator adds new functionalities to an object.
- Decorator focuses on enhancing individual objects, whereas Facade works on simplifying a set of interfaces in a subsystem.