Let's Play Around With Factory Method 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 in a web app.
 
Question: What is Factory Method Pattern?
 
In simple terms "An interface creates an object, but instantiation is dependent upon a child class".
 
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="FactoryMethodPatternApplication._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="form2" runat="server">

    <div>

        <h1 style="text-align: center; font-family: Verdana; font-size: large; color: Maroon">

            Factory Method 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="Button1_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2"><asp:Button ID="Button2" runat="server" Text="Subtraction" Width="165px" Font-Names="Verdana"
BackColor="Orange" OnClick="Button2_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2">

<
asp:Button ID="Button3" runat="server" Text="Multiplication" Width="165px" Font-Names="Verdana" BackColor="Orange" OnClick="Button3_Click" />
                    </td>

                </tr>

                <tr>

                    <td colspan="2">

<
asp:Button ID="Button4" runat="server" Text="Division" Width="165px" Font-Names="Verdana" BackColor="Orange" OnClick="Button4_Click" />
                    </td>

                </tr>

            </table>

            <table>

                <tr>

                    <td colspan="2">

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

               </tr>

            </table>

        </center>

    </div>

    </form>

</
body>
</
html>

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;namespace FactoryMethodPatternApplication

{

public interface IAdd{double Add(double a, double b);}

}

 

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

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;namespace FactoryMethodPatternApplication

{

public interface ISub{double Sub(double a, double b);}

}

 

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

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;namespace FactoryMethodPatternApplication

{

public interface IMul{double Mul(double a, double b);}

}

 

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

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;namespace FactoryMethodPatternApplication

{

public interface IDiv{double Div(double a, double b);}

}

 

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;namespace FactoryMethodPatternApplication

{

public class ClassAdd:IAdd{public double Add(double a, double b){return a + b;}}

}

 

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;namespace FactoryMethodPatternApplication

{

public class ClassSub:ISub{public double Sub(double a, double b){return a - b;}}

}

 

Step 8: The complete code of ClassMul.cs looks like this:
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;namespace FactoryMethodPatternApplication

{

public class ClassMul:IMul{public double Mul(double a, double b){return a * b;}}

}

 

Step 9: The complete code of ClassDiv.cs looks like this:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;namespace FactoryMethodPatternApplication

{

public class ClassDiv:IDiv{public double Div(double a, double b){return a / b;}}

}

 

Step 10: The complete code of IFactoryMethodArthmetic.cs looks like this:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;namespace FactoryMethodPatternApplication

{

public interface IFactoryMethodArthmetic{IAdd Add();ISub Sub();IMul Mul();IDiv Div();}

}

 

Step 11: The complete code of FactoryMethodArthmetic.cs looks like this:
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;namespace FactoryMethodPatternApplication

{

public class FactoryMethodArthmetic:IFactoryMethodArthmetic{public IAdd Add(){return new ClassAdd();}public ISub Sub(){return new ClassSub();}public IMul Mul(){return new ClassMul();}public IDiv Div(){return new ClassDiv();}}

}

 

Step 12: 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;

namespace FactoryMethodPatternApplication

{

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

    {

        protected void Page_Load(object sender, EventArgs e)

         {

      

        }

        public void Button1_Click(object sender, EventArgs e)

        { AddData();

        }

        public void Button2_Click(object sender, EventArgs e) { SubData();

        }

        public void Button3_Click(object sender, EventArgs e)

        {

            MulData();

        }

        public void Button4_Click(object sender, EventArgs e)

        {

            DivData();

        }

        public void AddData()

        {

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

            {

                lblResult.Text = "Please Enter Some Values";

            }

            else

            {

               IFactoryMethodArthmetic objArthmetic = new FactoryMethodArthmetic(); IAdd addFactory; addFactory = objArthmetic.Add(); lblResult.Text = "Addition Result of " + TextBox1.Text + " and " + TextBox2.Text + " is: <b>" +         addFactory.Add(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)) + "</b>"; TextBox1.Text = ""; TextBox2.Text = "";

            }

        } public void SubData()

        {

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

            {

                lblResult.Text = "Please Enter Some Values";

            }

            else

            { IFactoryMethodArthmetic objArthmetic = new FactoryMethodArthmetic(); ISub subFactory; subFactory = objArthmetic.Sub(); lblResult.Text = "Subtraction Result of " + TextBox1.Text + " and " + TextBox2.Text + " is: <b>" + subFactory.Sub(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)) + "</b>"; TextBox1.Text = ""; TextBox2.Text = "";

            }

        } public void MulData()

        {

            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text)) { lblResult.Text = "Please Enter Some Values";

        }

        else

            {

                IFactoryMethodArthmetic objArthmetic = new FactoryMethodArthmetic(); IMul mulFactory; mulFactory = objArthmetic.Mul(); lblResult.Text = "Multiplication Result of " + TextBox1.Text + " and " + TextBox2.Text + " is: <b>" + mulFactory.Mul(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)) + "</b>"; TextBox1.Text = ""; TextBox2.Text = "";

            }

        }

        public void DivData()

        {

            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text)) { lblResult.Text = "Please Enter Some Values";

            }

            else

            {

                IFactoryMethodArthmetic objArthmetic = new FactoryMethodArthmetic(); IDiv divFactory; divFactory = objArthmetic.Div(); lblResult.Text = "Division Result of " + TextBox1.Text + " and " + TextBox2.Text + " is: <b>" + divFactory.Div(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)) + "</b>"; TextBox1.Text = ""; TextBox2.Text = "";

            }

        }

    }

}

 

 

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

Output1.png

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


Output2.png

 

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


Output3.png

 

Step 16: The output of the division operation application looks like this:


Output4.png

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


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