->How to restrict object creation to only one objectaspx means u only assign one class one object(ussing singleton class)
->Singleton class:A singleton class is a class on which we can create only oneobject at any given point of time
->Also we r get only one O/P bec in singleton class we can get another O/p
->Singleton class:A singleton class is a class on which we can create only oneobject at any given point of time
->Also we r get only one O/P bec in singleton class we can get another O/p
Sample code:
public partial class How_to_restrict_object_creation_to_only_one_objectaspx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
B b1 = B.m(20);
B a1 = B.m(30);
Response.Write(a1.x);
Response.Write(b1.x);
}
}
public class B
{
public int x;
private static B a;
public static int count = 0;
private B(int x)
{
this.x = x;
}
public static B m(int y)
{
if (count == 0)
{
a = new B(y);
}
return a;
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
B b1 = B.m(20);
B a1 = B.m(30);
Response.Write(a1.x);
Response.Write(b1.x);
}
}
public class B
{
public int x;
private static B a;
public static int count = 0;
private B(int x)
{
this.x = x;
}
public static B m(int y)
{
if (count == 0)
{
a = new B(y);
}
return a;
}
}
No comments:
Post a Comment