Facade is structural design pattern. It hides the complexities of large system and provides a simple interface to client.
Client uses only Facade and it's not worried about inter dependencies of sub-systems.
Definition from Gang of Four book:
Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use
Structure:
Real world example:
Think of some travel booking sites like makemytrip, cleartrip which offers services to book Trains, Flights and Hotels.
Code snippet:
import java.util.*;
public class TravelFacade{
FlightBooking flightBooking;
TrainBooking trainBooking;
HotelBooking hotelBooking;
enum BookingType {
Flight,Train,Hotel,Flight_And_Hotel,Train_And_Hotel;
};
public TravelFacade(){
flightBooking = new FlightBooking();
trainBooking = new TrainBooking();
hotelBooking = new HotelBooking();
}
public void book(BookingType type, BookingInfo info){
switch(type){
case Flight:
// book flight;
flightBooking.bookFlight(info);
return;
case Hotel:
// book hotel;
hotelBooking.bookHotel(info);
return;
case Train:
// book Train;
trainBooking.bookTrain(info);
return;
case Flight_And_Hotel:
// book Flight and Hotel
flightBooking.bookFlight(info);
hotelBooking.bookHotel(info);
return;
case Train_And_Hotel:
// book Train and Hotel
trainBooking.bookTrain(info);
hotelBooking.bookHotel(info);
return;
}
}
}
class BookingInfo{
String source;
String destination;
Date fromDate;
Date toDate;
List<PersonInfo> list;
}
class PersonInfo{
String name;
int age;
Address address;
}
class Address{
}
class FlightBooking{
public FlightBooking(){
}
public void bookFlight(BookingInfo info){
}
}
class HotelBooking{
public HotelBooking(){
}
public void bookHotel(BookingInfo info){
}
}
class TrainBooking{
public TrainBooking(){
}
public void bookTrain(BookingInfo info){
}
}
Explanation:
FlightBooking, TrainBooking and HotelBooking
are different sub-systems of large system : TravelFacade
TravelFacade
offers a simple interface to book one of below options
Flight Booking
Train Booking
Hotel Booking
Flight + Hotel booking
Train + Hotel booking
book API from TravelFacade internally calls below APIs of sub-systems
flightBooking.bookFlight
trainBooking.bookTrain(info);
hotelBooking.bookHotel(info);
In this way, TravelFacade
provides simpler and easier API with-out exposing sub-system APIs.
Applicability and Use cases (from Wikipedia) :