SingleTon Object

Introduction

Sometime we need a class that has to be instantiated only once. That means we cannot have more than one instance for a class. Here singleton comes into picture. Singleton object is not a new thing come from oops concept. Let us read how easy it is.

Implementation

Typically every class is instantiated using constructor. Suppose if you make the access modifier of constructor to private, what happens u cannot access the constructor outside the class. That means u cannot instantiate your class. Then now you may have a doubt that how can we access the methods in that class without an instance. So for that you are creating one static method inside the class, I think now you got an idea. Yes, how can we access static method in a class? Directly using (classname.Methodname).
In the same way we are using our static method in our class. That static method is of class type it belongs. That means we are returning class object from static method. In this way we are restricting our class to single instanced class.

Example

This gives some clarification.public class MySingletonClass
{
//////// declarations

private string _name;
private string _company;
private static MySingletonClass singletonobject = new MySingletonClass();

//////// here some properties
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

public string Company
{
get
{
return _company;
}
set
{
_company = value;
}
}

///// here private constructor

private MySingleTonClass()
{
_name = “Phani”;
_company = “KST”;
}

public static MySingletonClass GetSingletonObject()
{
return singletonobject;

}

}

that’s all now u create singleton object in your application like this

MySingletonObject singleton = MySingletonObject.GetSingletonObject();
singleton.Name = “Phanindra”;
singleton.Company = “KST”;

 

 

 

 

Summary

This article describes what singleton object is and how can we make it ? and these
Singleton objects are mainly used as session objects. Just directly assign this reference
To System.Web.HttpContext.Current.Session[“SingletonSession”] = singleton;

Que : Which of the following is a Singleton object

             DataReader,SqlConnetion,SqlDataAdapter,DataSet

Ans :  DataReader

An instance of the DataReader cannot be created like DataReader dr=new DataReader() but only like DataReader dr=cmd.ExecuteReader() which returns an open datareader , this is to restrict only one instance of the dr per execution as the datareader has the behavior of exclusively locking up db connections until it reads all the records

 Courtesy: The Seo Guru, A Software Development Company, Best OOPS Blog Site, Link Submission, Thanks to Shopping  Site for Link Exchanging


Bookmark and Share

SingleTon Object

Introduction

Sometime we need a class that has to be instantiated only once. That means we cannot have more than one instance for a class. Here singleton comes into picture. Singleton object is not a new thing come from oops concept. Let us read how easy it is.

Implementation

Typically every class is instantiated using constructor. Suppose if you make the access modifier of constructor to private, what happens u cannot access the constructor outside the class. That means u cannot instantiate your class. Then now you may have a doubt that how can we access the methods in that class without an instance. So for that you are creating one static method inside the class, I think now you got an idea. Yes, how can we access static method in a class? Directly using (classname.Methodname).
In the same way we are using our static method in our class. That static method is of class type it belongs. That means we are returning class object from static method. In this way we are restricting our class to single instanced class.

Example

This gives some clarification.public class MySingletonClass
{
//////// declarations

private string _name;
private string _company;
private static MySingletonClass singletonobject = new MySingletonClass();

//////// here some properties
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

public string Company
{
get
{
return _company;
}
set
{
_company = value;
}
}

///// here private constructor

private MySingleTonClass()
{
_name = “Phani”;
_company = “KST”;
}

public static MySingletonClass GetSingletonObject()
{
return singletonobject;

}

}

that’s all now u create singleton object in your application like this

MySingletonObject singleton = MySingletonObject.GetSingletonObject();
singleton.Name = “Phanindra”;
singleton.Company = “KST”;

 

 

 

 

Summary

This article describes what singleton object is and how can we make it ? and these
Singleton objects are mainly used as session objects. Just directly assign this reference
To System.Web.HttpContext.Current.Session[“SingletonSession”] = singleton;

Que : Which of the following is a Singleton object

             DataReader,SqlConnetion,SqlDataAdapter,DataSet

Ans :  DataReader

An instance of the DataReader cannot be created like DataReader dr=new DataReader() but only like DataReader dr=cmd.ExecuteReader() which returns an open datareader , this is to restrict only one instance of the dr per execution as the datareader has the behavior of exclusively locking up db connections until it reads all the records

 Courtesy: The Seo Guru, A Software Development Company, Best OOPS Blog Site, Link Submission, Thanks to Shopping  Site for Link Exchanging


Bookmark and Share