Lets Play Around With Builder Design Pattern

Introduction
 

Today, in this article let's play around with one of the interesting and most useful concepts of design patterns, which will be hosted on a web app.
 
Question: What is Builder Pattern?

 

In simple terms "This patterns helps the construction of an object and its representations from a complex, cluttered environment to a simple, easy and understandable format. With the help of this pattern we can customize our objects and can be used accordingly towards the requirements".

 

I think we are now good to go and 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="BuilderDesignPatternApp.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">

            Builder 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" Width="165px" Font-Names="Verdana"

                            BackColor="Orange" OnClick="Button1Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2">

                        <asp:Button ID="Button2" runat="server" Text="Subtraction" Width="165px" Font-Names="Verdana"

                            BackColor="Orange" OnClick="Button2Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2">

                        <asp:Button ID="Button3" runat="server" Text="Multiplication" Width="165px" Font-Names="Verdana"

                            BackColor="Orange" OnClick="Button3Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2">

                        <asp:Button ID="Button4" runat="server" Text="Division" Width="165px" Font-Names="Verdana"

                            BackColor="Orange" OnClick="Button4Click" />

                    </td>

                </tr>

            </table>

            <table>

                <tr>

                    <td colspan="2">

                        <asp:Label ID="lblResult" runat="server" Font-Names="Verdana"></asp:Label>

                    </td>

                </tr>

            </table>

        </center>

    </div>

    </form>

</body>

</html>

 

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

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace BuilderDesignPatternApp

{

    public interface IArthmetic { double Arthmetic(double a, double b);}

}

 

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

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

namespace BuilderDesignPatternApp

{

    public class Addition : IArthmetic { public double Arthmetic(double a, double b) { return a + b; } }

}

 

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

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

namespace BuilderDesignPatternApp

{

    public class Substraction : IArthmetic { public double Arthmetic(double a, double b) { return a - b; } }

}

 

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

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

namespace BuilderDesignPatternApp

{

    public class Multiplication : IArthmetic {
    public double Arthmetic(double a, double b)
   {
     return a * b;
    }
  }

}

 

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

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

namespace BuilderDesignPatternApp

{

    public class Division : IArthmetic { public double Arthmetic(double a, double b) { return a / b; } }

}

 

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

 

using System;

using System.Globalization;namespace BuilderDesignPatternApp

{

    public partial class Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        TextBox1.Focus();

    }

    protected void Button1Click(object sender, EventArgs e)

    {

        if(string.IsNullOrEmpty(TextBox1.Text)||string.IsNullOrEmpty(TextBox2.Text))

        {

            lblResult.Text = "Please Enter Some Values";

            lblResult.ForeColor = System.Drawing.Color.Red;

        }

        else

        {

            lblResult.Text = "Addition Result of <b>" + TextBox1.Text + "</b> and <b>" + TextBox2.Text +"</b> is <b>" + DoArthmetic(_objAddition).ToString(CultureInfo.InvariantCulture) + "</b>";

            lblResult.ForeColor = System.Drawing.Color.Black;TextBox1.Text = "";

            TextBox2.Text = "";

        }

    }

    protected void Button2Click(object sender, EventArgs e)

    {

        if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))

        {

            lblResult.Text = "Please Enter Some Values";

            lblResult.ForeColor = System.Drawing.Color.Red;

        }

        else

        {

            lblResult.Text = "Substraction Result of <b>" + TextBox1.Text + "</b> and <b>" + TextBox2.Text +"</b> is <b>" + DoArthmetic(_objSubstraction).ToString(CultureInfo.InvariantCulture) + "</b>";

            lblResult.ForeColor = System.Drawing.Color.Black;TextBox1.Text = "";

            TextBox2.Text = "";

        }

    }

    protected void Button3Click(object sender, EventArgs e)

    {

        if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))

        {

            lblResult.Text = "Please Enter Some Values";

            lblResult.ForeColor = System.Drawing.Color.Red;

        }

        else

        {

            lblResult.Text = "Multiplication Result of <b>" + TextBox1.Text + "</b> and <b>" + TextBox2.Text +"</b> is <b>" + DoArthmetic(_objMultiplication).ToString(CultureInfo.InvariantCulture) + "</b>";

            lblResult.ForeColor = System.Drawing.Color.Black;TextBox1.Text = "";

            TextBox2.Text = "";

        }

    }

    protected void Button4Click(object sender, EventArgs e)

    {

        lblResult.Text = "";

        if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))

        {

            lblResult.Text = "Please Enter Some Values";

            lblResult.ForeColor = System.Drawing.Color.Red;

        }

        else{lblResult.Text = "Division Result of <b>" + TextBox1.Text + "</b> and <b>" + TextBox2.Text +"</b> is <b>"+DoArthmetic(_objDivision).ToString(CultureInfo.InvariantCulture)+"</b>";

            lblResult.ForeColor = System.Drawing.Color.Black;

            TextBox1.Text = "";

            TextBox2.Text = "";

        }

   

        private double DoArthmetic(IArthmetic arthmetic)

        {

            double d = arthmetic.Arthmetic(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text));

            return d;

        }

    #region

    Private Instance Variblesprivate readonly IArthmetic _objAddition = new Addition();

    private readonly IArthmetic _objSubstraction = new Substraction();

    private readonly IArthmetic _objMultiplication = new Multiplication();

    private readonly IArthmetic _objDivision = new Division();#endregion

}

}

 

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


 

Output1.png
 

 

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


 

Output2.png
 

 

Step 10: The output of the addition operation application looks like this:


 

Output3.png
 

 

Step 11: The output of the multiplication operation application looks like this:

  Output4.png

I hope this article is useful for you.


MVC Corporation
MVC Corporation is consulting and IT services based company.