Design Patterns
Example 2: Hotel Booking System

In a hotel booking system, different subsystems could handle room availability, booking confirmation, and payment processing. Here’s how a Facade might simplify the interaction:


// Subsystem classes
class RoomAvailability
{
    public bool CheckAvailability(string roomId, DateTime startDate, DateTime endDate)
    {
        // Simulated room availability check logic
        Console.WriteLine($"Room {roomId} is available from {startDate} to {endDate}.");
        return true;
    }
}

class BookingConfirmation
{
    public void ConfirmBooking(string roomId, DateTime startDate, DateTime endDate, string guestName)
    {
        // Simulated booking confirmation logic
        Console.WriteLine($"Booking confirmed for room {roomId} from {startDate} to {endDate} for guest {guestName}.");
    }
}

class PaymentProcessor
{
    public void ProcessPayment(decimal amount, string guestName)
    {
        // Simulated payment processing logic
        Console.WriteLine($"Payment of ${amount} processed for guest {guestName}.");
    }
}

// Facade class
class HotelBookingFacade
{
    private RoomAvailability roomAvailability;
    private BookingConfirmation bookingConfirmation;
    private PaymentProcessor paymentProcessor;

    public HotelBookingFacade()
    {
        roomAvailability = new RoomAvailability();
        bookingConfirmation = new BookingConfirmation();
        paymentProcessor = new PaymentProcessor();
    }

    public void BookHotelRoom(string roomId, DateTime startDate, DateTime endDate, string guestName, decimal amount)
    {
        Console.WriteLine($"Booking hotel room {roomId} for guest {guestName}:");

        if (roomAvailability.CheckAvailability(roomId, startDate, endDate))
        {
            bookingConfirmation.ConfirmBooking(roomId, startDate, endDate, guestName);
            paymentProcessor.ProcessPayment(amount, guestName);
        }
        else
        {
            Console.WriteLine($"Room {roomId} is not available for the selected dates.");
        }
    }
}

// Client code
class Program
{
    static void Main(string[] args)
    {
        HotelBookingFacade bookingFacade = new HotelBookingFacade();

        // Client books a hotel room
        bookingFacade.BookHotelRoom("101", DateTime.Now.AddDays(1), DateTime.Now.AddDays(3), "John Doe", 300);
    }
}

In these examples:

  • Facade (MortgageApplicationFacade, HotelBookingFacade) simplifies complex interactions with multiple subsystems (CreditChecker, DocumentVerifier, ApprovalProcess in the first example; RoomAvailability, BookingConfirmation, PaymentProcessor in the second example).
  • Client code interacts with the Facade (ApplyForMortgage, BookHotelRoom), which in turn coordinates actions across the relevant subsystems to achieve the desired functionality.
Summary

In summary, the Facade pattern is used to provide a simple interface to a complex subsystem, making it easier for clients to use the system without needing to understand its intricacies. It's particularly useful in large applications where subsystems can be vast and their interactions complex.