What is the Facade Design Pattern?
Table of contents
The Facade Design Pattern is a structural pattern that provides a unified interface to a set of interfaces in a subsystem, making it easier to use. The Facade pattern simplifies the interaction with complex systems by providing a simplified interface to the underlying code. It is used to hide the complexity of a system and make it easier to use for clients.
Explanation
The Facade pattern is designed to provide a simple interface to a complex system. It is a way to simplify the interaction with a system by providing a simplified interface. This interface is then used by clients to access the system without having to know the details of how it works.
The Facade pattern is implemented by creating a Facade class that provides a simple interface to the underlying system. This class is responsible for managing the interactions between the client and the system. It hides the complexity of the system and provides a simplified interface that clients can use.
Client
|
v
Facade Class
|
---------------------
| |
Subsystem Class A Subsystem Class B
Examples
Consider a scenario where a client wants to book a flight. To do so, the client needs to interact with several subsystems, such as the airline reservation system, the payment gateway, and the airport system. Without a Facade pattern, the client would have to interact with each of these subsystems separately, which can be complex and time-consuming.
However, with the Facade pattern, a Facade class can be created to manage the interactions with these subsystems. The Facade class provides a simple interface that the client can use to book a flight. The Facade class then interacts with the airline reservation system, the payment gateway, and the airport system on behalf of the client.
Here is an example code snippet of the Facade pattern:
class FlightBookingFacade {
private ReservationSystem reservationSystem;
private PaymentGateway paymentGateway;
private AirportSystem airportSystem;
public FlightBookingFacade() {
reservationSystem = new ReservationSystem();
paymentGateway = new PaymentGateway();
airportSystem = new AirportSystem();
}
public void bookFlight(String source, String destination, Date departureDate, Date returnDate) {
// Check availability
if (!reservationSystem.checkAvailability(source, destination, departureDate, returnDate)) {
throw new RuntimeException("No flights available for the given dates");
}
// Book flight
reservationSystem.book(source, destination, departureDate, returnDate);
// Make payment
paymentGateway.makePayment();
// Issue ticket
airportSystem.issueTicket();
}
}
In this example, the FlightBookingFacade
class is responsible for managing the interactions with the reservation system, payment gateway, and airport system. The bookFlight
method provides a simple interface that the client can use to book a flight. The method then interacts with the subsystems on behalf of the client, hiding the complexity of the system.
Benefits
The Facade pattern provides several benefits, including:
Simplifies the interface to a complex system, making it easier for clients to use.
Hides the complexity of the system from clients, reducing the risk of errors and improving maintainability.
Encapsulates the interactions with the subsystems, making it easier to make changes to the system without affecting clients.
Conclusion
The Facade Design Pattern is a useful pattern that simplifies the interaction with complex systems by providing a simplified interface. It hides the complexity of the system from clients and encapsulates the interactions with the subsystems, making it easier to make changes to the system without affecting clients. By using the Facade pattern, developers can create more maintainable, reliable, and scalable systems.