Pages

Sunday, February 7, 2016

Design Patterns : Facade Pattern

You may also like to see:

Facade design pattern is a structural design pattern. Word facade means 'a deceptive outward appearance'. As facade design pattern hides all the complexities of one or many sub-systems and provides a clean, easy to use facade interface.

We can create multiple Facade interfaces by grouping relevant sub-system in particular class. The client will communicate these facades instead of complex individual subsystems.

Facade Design Pattern - Class Diagram

Implementation

Let's implement a facade pattern on an e-commerce site. We are going to create an online shopping system in which user selects an item from a catalog and place an order. Let's see its code

public class Inventory 
{
    public bool IsItemAvailable(String itemId) 
    {
       return "Item details";
    }
}

public class Payment 
{
    public String Pay(String itemId,Paypal paypal)
    {
       return "Charge for items";
    }
}

public class Shipping 
{
    public String ShipOrder(String itemId,Address shipmentAddress) 
    {
       return "Ship items to address";
    }
}

public class OrderFacade 
{
    // composed of all Order related sub-systems
    Payment _Payment;
    Inventory _Inventory;
    Inventory _Shipping;

    public void OrderFacade()
    {
        _Payment = new Payment();
        _Inventory = new Inventory();
        _Shipping = new Shipping();
    }

    public void PlaceOrder(String itemId,string creditCard,string shipmentAddress) 
    {
      // check if item is available 
      if(!_Inventory.IsItemAvailable(itemId)) return "Item Not Available";
      // charge for item price
      _Payment.Pay(itemId, new Paypal(creditCard));
      // ship order to customer
      _Shipping.ShipOrder(itemId,shipmentAddress) 
    }
}

public class Client 
{
    public static void main()
    {
       OrderFacade orderFacade = new OrderFacade();
       orderFacade.PlaceOrder("1234", "321546", "23-B Frim Road, Moscow");         
    }
}


As you have seen OrderFacade has encapsulated all the order-related activities and now client just has single point to communicate instead of invoking each sub-system individually. OrderFacade will delegate relevant sub-system and will complete the client request.

Facade pattern provides an interface which is easy to use for a client by hiding many interfaces. This pattern is actually straightforward but it is quite powerful. It allows us to make our system loosely coupled. There is a design principle Least Knowledge which guide us to have fewer dependencies between objects which mean objects should not be tightly coupled otherwise it would be difficult to manage.

Using Facade pattern as we know we have an interface which is responsible for communicating between the objects so it actually allows us to have fewer dependencies within objects. It has many advantages for instance in our example of online shopping system if our payment system is completely independent we can easily change it anytime without affecting the client.

Conclusion


Facade design pattern is a structural pattern which makes design easy by allowing to make a system less tightly coupled and provide an interface to the client which is clean and easy to use by hiding all complexities of sub-systems.


You may also like to see:

1 comment:

  1. This explanation gives basic idea which is enough to know the details,thanks for sharing

    ReplyDelete