Reader Level:
Articles

Let's Play Around With Facade Pattern - Design Pattern

By Vijay Prativadi on April 16, 2012
Today, in this article let's play around with an interesting and one of the most useful concepts of design pattern, which will be hosted in a web app.
  • 0
  • 0
  • 3674

Introduction

Today, in this article let's play around with an interesting and one of the most useful concepts of design pattern, which will be hosted in a web app.

Question:
What is Facade Pattern?

In simple terms "It acts as a single set of unified wrappers for multiple complex classes, it wraps a bunch of complex classes into a single unique facade pattern class. By doing this we can minimize complexity faced when code keeps growing and growing. It provides a more sophisticated look when even looked at class diagram and easy understandability ".

The diagrammatic representation of the Facade Pattern - Design Pattern for this application looks like this:

Image1.png

I think we are now good to go to implement this wonderful concept.

Step 1:
The complete code of Default.aspx looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FacadePatternApplication._Default" %>
 
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <h1 style="text-align: center; font-family: Verdana; font-size: large; color: Maroon">
            Facade Pattern - Design Patterns</h1>
        <center>
            <table
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="Please Enter First Number" Font-Size="Small"
                            Font-Bold="true" Font-Italic="true" Font-Names="Verdana"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Please Enter Second Number" Font-Size="Small"
                            Font-Bold="true" Font-Italic="true" Font-Names="Verdana"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button ID="Button1" runat="server" Text="Addition / Subtraction" Width="165px"
                            Font-Names="Verdana" BackColor="Orange" OnClick="Button1_Click" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button ID="Button2" runat="server" Text="Multiply / Divide" Width="165px" Font-Names="Verdana"
                            BackColor="Orange" OnClick="Button2_Click" />
                    </td>
                </tr>
            </table>
            <table>
                <tr>
                    <td colspan="2">
                        <asp:Label ID="lblResult" runat="server" Font-Names="Verdana" ForeColor="Brown"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Label ID="lblResult1" runat="server" Font-Names="Verdana" ForeColor="Brown"></asp:Label>
                    </td>
                </tr>
            </table>
        </center>
    </div>
    </form>
</body>
</
html>

Step 2: The complete code of PartialAddClass.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FacadePatternApplication
{
    public class PartialAddClass
    {
        public double Add(double a, double b)
        {
            return a + b;
        }
    }
}

Step 3: The complete code of PartialSubClass.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FacadePatternApplication
{
    public class PartialSubClass
    {
        public double Sub(double a, double b)
        {
            return a - b;
        }
    }
}

Step 4: The complete code of PartialMulClass.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FacadePatternApplication
{
    public class PartialMulClass
    {
        public double Mul(double a, double b)
        {
            return a * b;
        }
    }
}


Step 5:
The complete code of PartialDivClass.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FacadePatternApplication
{
    public class PartialDivClass
    {
        public double Div(double a, double b)
        {
            return a / b;
        }
    }
}


Step 6: The complete code of Default.aspx.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication3;
 
namespace FacadePatternApplication
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))
            {
                lblResult.Text = "Please Enter Some Values"; lblResult1.Text = "";
            }
            else
            {
                AddSub(); TextBox1.Text = ""; TextBox2.Text = "";
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))
            {
                lblResult.Text = "Please Enter Some Values"; lblResult1.Text = "";
            }
            else
            {
                MulDiv(); TextBox1.Text = ""; TextBox2.Text = "";
            }
        }
        public void AddSub()
        {
            lblResult.Text= "Addition Result is <b>"+objAdd.Add(Convert.ToDouble(TextBox1.Text),
                Convert.ToDouble(TextBox2.Text))+"</b></br>";
            lblResult1.Text = "Subtraction Result is <b>" + objSub.Sub(Convert.ToDouble(TextBox1.Text),
                Convert.ToDouble(TextBox2.Text)) + "</b></br>";
        }
        public void MulDiv()
        {
            lblResult.Text = "Multiplication Result is <b>" + objMul.Mul(Convert.ToDouble(TextBox1.Text),
                Convert.ToDouble(TextBox2.Text)) + "</b></br>";
            lblResult1.Text = "Division Result is <b>" + objDiv.Div(Convert.ToDouble(TextBox1.Text),
                Convert.ToDouble(TextBox2.Text)) + "</b></br>";
        }
        #region Instance Variables PartialAddClass objAdd = new PartialAddClass();
        PartialSubClass objSub = new PartialSubClass();
        PartialMulClass objMul = new PartialMulClass();
        PartialDivClass objDiv = new PartialDivClass();
        #endregion
    }
}

Step 7: The output of the application looks like this:

Output1.png

Step 8: The output of the nothing entered application looks like this:

Output2.png

Step 9: The output of the addition/subtraction operation application looks like this:

Output3.png
Output4.png

Step 10: The output of the multiplication/division operation application looks like this:

Output5.png

I hope this article is useful for you. I look forward for your comments and feedback. Thanks Vijay Prativadi

Vijay Prativadi

Hey Everyone, It's Vijay. I am SDE mostly up on Microsoft Technologies. If you are thinking to know what I do mostly when at work. I get a chance to work upon few Microsoft technologies. All the way along my core tech... Read more

COMMENT USING

Trending up