Design Patterns Series 10 - Facade Pattern

Similar to Adapter pattern, that we explored in the previous post, there is another design pattern that work in much the same way, but it has different purpose.

Facade Pattern :

The facade pattern gives you a wrapper that makes the original code easier to deal with.It makes an OOP interface easier to use. it's fundamentally a design issue - if an object or class interface is too hard to work with, the facade pattern gives you a front end to that interface to make it easier.

The facade pattern should provide a unified interface to a set of interfaces in a system. Facade defines a higher level interface that makes the subsystem easier to use. The idea is simple; a facede just simplifies the interface between a class or object and the code that makes use of that class or object.

You usually use the facade pattern when you cannot rewrite the code you wish were simpler. Although using a facade can fix the problem, but it adds another layer, and if the underlying code changes, you're going to have to change your facade pattern as well.

The idea is that for effective OOP, you don't want to insist that separate entities have to know too much about each other. As much as possible, you should lock away the details inside each class or object and make the coupling between entities as loose as possible. If one object needs to know too much about another to make their coupling loose, a Facade pattern can help.

To see how facade pattern works, assume you want to get the available hotels from hotels repository in certain dates. Getting availability process starts with validating the user input,get nearest hotels, check rooms types in each hotel, and return the available room in each hotel. As you see, getting hotel availability is a complicated process which contains many steps.
public class HotelAvailability
{
   public Boolean ValidateInputs(RequestInput inputs)
   {
      // validate inputs and return true or false
   }
   public List<Hotel> NearestHotel(RequestInput inputs)
   {
      // return list of nearest hotels
   }
   public List<Hotel> CheckRoomTypes(List<Hotel> nearestHotels)
   {
      // Filter nearest hotels according to room types
   }
   public List<Hotel> GetAvailabileHotels(List<Hotel> hotelsWithCheckedRooms)
   {
      // Prepare the hotel availability result
   }
}
To use this class for getting hotel availability
HotelAvailability availObj = new HotelAvailability();
if(availObj.ValidateInputs(inputs))
{
   List<Hotel> nearsetHotels = availObj.NearestHotel(inputs);
   List<Hotel> hotelsWithRoomsTypes = availObj.CheckRoomTypes(nearsetHotels );
   List<Hotel> availResult = availObj.GetAvailabileHotels(hotelsWithRoomsTypes);
}
It is so hard to get availability.To make it simple you can use facade pattern by creating a new class called Availability that play as an interface between HotelAvailability class and the code where we use it.We will pass the inputs object into new class constructor.
Public class Availability
{
   private RequestInput inputs;
   public Availability(RequestInput _inputs)
   {
      inputs = _inputs
   }
   public List<Hotel> GetHotelAvailability()
   {
      HotelAvailability availObj = new HotelAvailability();
      if(availObj.ValidateInputs(inputs))
      {
         List<Hotel> nearsetHotels = availObj.NearestHotel(inputs);
         List<Hotel> hotelsWithRoomsTypes = availObj.CheckRoomTypes(nearsetHotels );
         List<Hotel> availResult = availObj.GetAvailabileHotels(hotelsWithRoomsTypes);
         return availResult;
      }
      else
      {
         return null;
      }
   }
}
To get hotel availability using facade pattern you just have to make one call to method GetHotelAvailabilty
Availability availObj = new Availability(inputs);
List<Hotel> hotelsList = availObj.GetHotelAvailabilty()

Comments

  1. i love your blog, i have it in my rss reader and always like new things coming up from it.

    ReplyDelete
    Replies
    1. Thanks and you are welcome. I will do my best to write a helpful posts.

      Delete

Post a Comment

Popular posts from this blog

ASP.Net MVC : ActionLink with bootstrap icon

ASP.Net MVC : Conditional Validation using ValidationAttribute

Android : How to change progress bar color at runtime programmatically?