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 Adapter Pattern?
In simple terms "When working with incompatible interfaces, an adapter pattern helps to synchronize and make the classes compatible among each other".
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="AdapterDesignPatternApp.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">
Adapter 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 IAdd.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AdapterDesignPatternApp
{
public interface IAdd { void Add();}
}
Step 3: The complete code of ISub.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AdapterDesignPatternApp
{
public interface ISub { void Sub();}
}
Step 4: The complete code of IMul.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AdapterDesignPatternApp
{
public interface IMul { void Mul();}
}
Step 5: The complete code of IDiv.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AdapterDesignPatternApp
{
public interface IDiv { void Div();}
}
Step 6: The complete code of MyAddition.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AdapterDesignPatternApp
{
public class MyAddition { public double Addition(double a, double b) { return a + b; } }
}
Step 7: The complete code of MySubtraction.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AdapterDesignPatternApp
{
public class MySubtraction { public double Subtraction(double a, double b) { return a - b; } }
}
Step 8: The complete code of MyMultiplication.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AdapterDesignPatternApp
{
public class MyMultiplication { public double Multiplication(double a, double b) { return a * b; } }
}
Step 9: The complete code of MyDivision.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AdapterDesignPatternApp
{
public class MyDivision { public double Division(double a, double b) { return a / b; } }
}
Step 10: The complete code of Default.aspx.cs looks like this:
using System;
using System.Globalization;
namespace AdapterDesignPatternApp
{
public partial class Default : System.Web.UI.Page, IAdd, ISub, IMul, IDiv
{
protected void Page_Load(object sender, EventArgs e){TextBox1.Focus();
}
protected void Button1Click(object sender, EventArgs e){Add();
}
protected void Button2Click(object sender, EventArgs e){Sub();
}
protected void Button3Click(object sender, EventArgs e){Mul();
}
protected void Button4Click(object sender, EventArgs e){Div();
}
public void Add()
{
if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text)){lblResult.Text = "Please Enter Some Values";
lblResult.ForeColor = System.Drawing.Color.Red;
{
lblResult.Text = "Addition Result is: <b>"+TextBox1.Text+"</b>"+" and "+TextBox2.Text+" is <b>"+_objAddition.Addition(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)).ToString(CultureInfo.InvariantCulture)+"</b>";
lblResult.ForeColor = System.Drawing.Color.Green;TextBox1.Text = "";TextBox2.Text = "";
}
}
public void Sub()
{
if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text)){lblResult.Text = "Please Enter Some Values";
lblResult.ForeColor = System.Drawing.Color.Red;
}
else
{
lblResult.Text = "Subtraction Result is: <b>" + TextBox1.Text + "</b>" + " and " + TextBox2.Text + " is <b>" + _objSubtraction.Subtraction(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)).ToString(CultureInfo.InvariantCulture) + "</b>";lblResult.ForeColor = System.Drawing.Color.Green;TextBox1.Text = "";
TextBox2.Text = "";
}
}
public void Mul()
{
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 is: <b>" + TextBox1.Text + "</b>" + " and " + TextBox2.Text + " is <b>" + _objMultiplication.Multiplication(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)).ToString(CultureInfo.InvariantCulture) + "</b>";
lblResult.ForeColor = System.Drawing.Color.Green;TextBox1.Text = "";TextBox2.Text = "";
}
}public void Div()
{
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 is: <b>" + TextBox1.Text + "</b>" + " and " + TextBox2.Text + " is <b>" + _objDivision.Division(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)).ToString(CultureInfo.InvariantCulture) + "</b>";
lblResult.ForeColor = System.Drawing.Color.Green;
TextBox1.Text = "";TextBox2.Text = "";
}
}
#region Private Instance Variablesprivate readonly MyAddition _objAddition = new MyAddition();
private readonly MySubtraction _objSubtraction = new MySubtraction();
private readonly MyMultiplication _objMultiplication = new MyMultiplication();
private readonly MyDivision _objDivision = new MyDivision();#endregion
}
Step 11: The output of the application looks like this:
Step 12: The output of the nothing entered application looks like this:
Step 13: The output of the addition operation application looks like this:
Step 14: The output of the multiplication operation application looks like this:
I hope this article is useful for you.