Thursday, February 16, 2012

Design Pattern : Façade pattern Implementation

Facade


The Facade Design Pattern provides a simple interface and controls access to a series of complicated interfaces and or sub systems.

Some of the important features and advantages of Facade design Pattern are

  • A facade can make a software library easier to use and understand, since the facade has convenient methods for common tasks
  • A facade can make code that uses the library more readable, for the same reason
  • A facade can reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system
  • A facade can wrap a poorly-designed collection of APIs with a single well-designed API


Challenge

You are working on a database application. Frequently you need to execute UPDATE and DELETE SQL queries. Each time there is a need to create the SqlConnection and SqlCommand class, assign the connection to command and execute the query.

The series of activities looks like:

    SqlConnection connection = new SqlConnection("connection string" );
    SqlCommand command = new SqlCommand();
    command.Connection = connection;
    command.CommandText = "UPDATE Customer SET Processed=1";
    command.ExecuteNonQuery();


You need to repeat the same code wherever execute queries are required. How to make this code better?

Definition

"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."

Implementation

Using Façade, we can improve the situation. We can find only the query is different in each case – the parameters like connection string is common for the entire application.

SQLFacade.ExecuteSQL("UPDATE query here..");

After using the Façade the code will look like above.

The complicated code is being pulled to the background SQLFacade class.

namespace FacadePattern
{
    public class SQLFacade
    {
        public static bool ExecuteSQL(string sql)
        {
            SqlConnection connection = new SqlConnection("connection string");
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = sql;
            command.ExecuteNonQuery();

            return true;
        }
    }
}


Façade pattern makes code better as depicted in the image below:

FacadePtrn.jpg

Summary

In this article we have seen how to use the Façade pattern to improve our code. It is similar to the use of reusable functions and it pulls out the complications and provides an easier interface for use. The associated code contains the classes we have discussed.

Another example: 
In the following example, a developer using the OrderFacade does not need to understand all the requirements of creating a proper Order class.  These details are hidden away behind the facade.
using System;
using System.Collections.Generic;


namespace Yanesh.DesignPatterns.Facade
{
    public class OrderFacade
    {
        //Places an Order and Returns an Order ID
        public int placeOrder(int CustomerID, List<BasketItem> Products)
        {
            Order anOrder = new Order();
            OrderLine anOrderLine = new OrderLine();
            Address DespatchAddress = Address.getCustomerDespatchAddress(CustomerID);
            int OrderId = anOrder.requestOrderID();

            anOrder.createOrder(OrderId, DespatchAddress);
            anOrderLine.addOrderLinesToOrder(OrderId, Products);

            return OrderId;
        }
    }

    //order class
    public class Order
    {
        public int requestOrderID()
        {
            //Creates and Returns a Unique Order ID        }

        public void createOrder(int OrderId, Address DespatchAddress)
        {
        }
    }

    //OrderLine Class
    public class OrderLine
    {
        public void addOrderLinesToOrder(int OrderId, List<BasketItem> Products)
        {
        }
    }
    
    //Public Customer
    public class Address
    {
        public static Address getCustomerDespatchAddress(int CustomerID)
        {
            return new Address();
        }
        //Address properties
    }

    public class BasketItem
    {
        //BasketItem Properties
    }
}
 
Another example:
Façade pattern sits on the top of group of subsystems and allows them to communicate in a unified manner.


Figure: - Façade and Subsystem

Figure ‘Order Façade’ shows a practical implementation of the same. In order to place an order we need to interact with product, payment and invoice classes. So order becomes a façade which unites product, payment and invoice classes.


Figure: - Order Facade

Figure ‘façade in action’ shows how class ‘clsorder’ unifies / uses ‘clsproduct’,’clsproduct’ and ‘clsInvoice’ to implement ‘PlaceOrder’ functionality.


Figure :- Façade in action
 Reference:

No comments:

Post a Comment