Friday, August 21, 2009

Facade Pattern in c#

Intent
The Facade defines a unified, higher level interface to a subsystem, that makes it easier to use.

Non-Software Example
When ordering from a catalog (over phone), consumers do not have direct contact with the Order Fulfillment, Billing, and Shipping departments. The Customer Service Representative acts as a Facade, or unified interface, to each of the departments involved in the transaction.



Benefits:

Clients are shielded from individual departments. When ordering an item, it is not necessary to check the stock, generate an invoice, and arrange for shipping. All steps are accomplished through the customer service representative. The internal structure of the organization can be changed without affecting the client. For example, the shipping department can be contracted to another organization, without impacting the client’s interface with the company.


UML:



Let us understand the UML. The above Fig shows an implementation of Façade pattern and below is the classes involved.

Façade: The Façade contains simple methods that can be accessible by the users and easily understood by the users. The Façade hides the complexities of the sub systems.

Package A & Package B: These are the packages that the user needs to implement. They may not necessarily reside in a single assembly.

Class A1, A2, B1, B2: These are the classes whose functionality the user wants to use. The façade expose these complex functionalities. The end user may not need to know the implementation details of these classes.

Software Example & Explanation of Code:

Let us go ahead with our previous example. Let say you have a system (console application) which is being used in an organization. You have modules like “Order Fulfilling” where order details are entered, “Billing” where payment details are entered and “Shipping” where shipment details are entered. Each of these modules is complex by nature and they are interdependent like shipping can only happen if Billing is complete etc. Earlier each department use to enter these details once they get an order. Now the organization has grown and now they have a customer service desk that takes the order and makes the entry in the system. So you need to build an interface for the service desk. So using Façade pattern you can design a class which will process the order without know the complexities of the individual modules.

Note: This sample is only for better understanding of the concept only.
So as in previous case each department will use their own code to do the task. Like the Order fulfillment will take the order and do the needful. Similarly billing and shipping department will do the needful by taking the data. Remember all of them use the “Billing”, “Shipping” and “OrderFullfillment” classes as shown below.

OrderFullFillment.cs

namespace FacadePattern
{
public class OrderFullFillment
{
//Here the total items available is 100. if the
//total requested is more it will return false
private int _intStock = 100;

public bool IsAvailable(int intOrdered)
{
if (_intStock > intOrdered)
Console.WriteLine("Order Placed Successfully");
else
Console.WriteLine("Order Failed");

return _intStock > intOrdered;
}
}
}

Billing.cs

namespace FacadePattern
{
public class Billing
{
public string CardNo
{
get;
set;
}
public int Amount
{
get;
set;
}
public bool BookTicket()
{
//Insert details to relevant tables
//with CardNo and amound
Console.WriteLine("Billing Details Updated Sussessfully");
return true;
}
}
}

Shipping.cs

namespace FacadePattern
{
public class Shipping
{
public string Adress
{
get;
set;
}
public void LogShipping()
{
Console.WriteLine("Product Shipped to " + this.Adress);
}
}
}

Now with the new requirement the ordering proceess has been changed and it has gone to the customer service desk. Now we need to build a class which can consume all these functionalities by providing a simple interface to the user (here user is some one who is going to use the class). This class would be our Façade class and the code for the class would look like this

namespace FacadePattern
{
public class FacadeClass
{
public void CreateOrder(int intQuantities,string strCardNo, int intAmount,string strAddress)
{
OrderFullFillment objOrder = new OrderFullFillment();
Billing objBilling = new Billing(strCardNo,intAmount);
Shipping objShipping = new Shipping(strAddress);

objBilling.CardNo = strCardNo;
objBilling.Amount = intAmount;

objShipping.Address = strAddress;
//Check whether stock available
if(objOrder.IsAvailable(intQuantities))
{
//Check payment Details
if (objBilling.BookOrder())
{
objShipping.LogShipping();
}
}
}
}
}

Now let us create client for the class. And pass different parameters

namespace FacadePattern
{
class Program
{
static void Main(string[] args)
{
FacadeClass objFacade = new FacadeClass();
objFacade.CreateOrder(80, "8827728829", 2000, "MyAddress");
Console.ReadLine();
}
}
}
When the order is less then 100 it givs the following results
Order Placed Successfully
Card No—8827728829 Amount--2000
Billing Details Updated Sussessfully
Product Shipped to MyAddress

If you change the value and pass the first parameter as 120 (basically any value more than 100), you will receive the following messages
“Order Failed”.
Basically what we have achieved with this is that with the façade we were able to hide the complexities of all the code and provided just once interface fro the user to create the order.

No comments: