SIGN UP MEMBER LOGIN:    
ARTICLE

Facade Pattern

Posted by Jean Paul Articles | Design & Architecture December 23, 2011
In this article I would like to explore the Façade design pattern. The actual pronunciation is fu’saad. The dictionary meaning of the word Façade is “A showy misrepresentation intended to conceal something unpleasant”
Reader Level:

In this article I would like to explore the Facade design pattern. The actual pronunciation is fu'saad. The dictionary meaning of the word Façade is "A showy misrepresentation intended to conceal something unpleasant"

Now let us enter the challenge.

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.

Resources

Here are some useful related resources:


 

Login to add your contents and source code to this article
share this article :
post comment
 

Thank You Sujith for the good words.. Your comment too makes it the most commented article of mine. Thanks :)

Posted by Jean Paul Jan 05, 2012

Hi Very nice and simple article on FACADE design pattern Regards Sujeet

Posted by Sujit Bhujbal Jan 04, 2012

Hei Vineet.. Thanks for the Good Words Yaar. I hope to be better with patterns n' examples.

Posted by Jean Paul Dec 27, 2011

Hi Akash.. Thanks for the comments.. I believe already we are using Facade without knowing the pattern name.

Posted by Jean Paul Dec 27, 2011

Thank You Arjun for the good words

Posted by Jean Paul Dec 27, 2011
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor