Everything you just saw in this article is only a small part of the book Introduction to design patterns, the most complete book of design patterns in Spanish, we cover 25 design patterns along with 25 real-world projects. Forget about learning design patterns with typical Internet examples of how to make pizza, animals, and geometric shapes. I invite you to see my book:See book
The Facade pattern has the particularity of hiding the complexity in interacting with a group of subsystems by providing a high level interface, which is in charge of establishing communication with all the subsystems. Using a facade is a good strategy for interacting with multiple subsystems in order to execute a concrete action, because we would need a technical and operational knowledge on each of them to be aware of which subsystem we need to run and in which order, and that can get too complicated when working with an overgrown system.
In the illustration, we can see the components included in the Facade pattern, which are explained as follows:
- IFacade: Provides a high-level interface for hiding the complexity of interacting with multiple systems to carry out an operation.
- Client: System or event which interacts with the facade.
- DefaultFacadeImpl: Represents the implementation of IFacade, which is in charge of communicating with all the subsystems.
- Subsystems: Represents all the modules or subsystems with interfaces for communication.
- The client invokes a facadeoperation.
- The facade communicates with SubsystemA to perform an operation.
- The facade communicates with SubsystemB to perform an operation.
- The facade communicates with SubsystemC to perform an operation.
- The facade provides the client with the result of the operation.
By implementing the Facade design pattern, we are going to develop a system for processing online payments, which would need to interact with different subsystems, each of them bringing their own complexities. This can be a difficult task, especially when we don't have information about the context of each subsystem. For that reason, we will implement a facade to show high-level operations that are going to be in charge of interacting with the subsystems, abstracting us, the programmers, from dealing with the intricacies of these interactions.