IMPLEMETN THE INTERFACES AND USE IT FACTORY PATTERN BASE CODE
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public interface myinterface
{
int add(int x,int y);
//int mul(int w, int z);
}
public class class1 : myinterface
{
public int add(int a, int b)
{
int c = a + b;
return c;
}
}
public class class2 : myinterface
{
public int add(int j, int k)
{
int l = j * k;
return l;
}
}
class Factory
{
public myinterface GetObject(int type)
{
myinterface base1 = null;
switch (type)
{
case 1:
base1 = new class1();
break;
case 2:
base1 = new class2();
break;
}
return base1;
}
}
public partial class _Default : System.Web.UI.Page
{
Factory factory = new Factory();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
myinterface mytest = factory.GetObject(1);
int result = mytest.add(Convert.ToInt32(TextBox1.Text.ToString()), Convert.ToInt32(TextBox2.Text.ToString()));
txtresult.Text = Convert.ToString(result);
}
protected void Button2_Click(object sender, EventArgs e)
{
myinterface multiply = factory.GetObject(2);
int multiplevalue = multiply.add(Convert.ToInt32(TextBox1.Text.ToString()), Convert.ToInt32(TextBox2.Text.ToString()));
txtmultiply.Text = Convert.ToString(multiplevalue);
}
}
Sunday, July 27, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment