Purpose :
Singletone is generally use for object optimization purpose. A project to develop a software where number of object is an important issue and the software weight is an important issue we use Single tone.
How :
Step 1: Create a class
Example:
class SingletoneExample
{
}
Step 2: Set the constructor as private.
Example:
class SingletoneExample
{
private SingletoneExample()
{
}
}
Step 3: Create class type reference variable.
Example :
class SingletoneExample
{
private SingletoneExample instance;
private SingletoneExample()
{
}
}
Step 4: Create a method. Set the method type as Class Type.create a object with the reference of class type varible and return it.
class SingletoneExample
{
private SingletoneExample instance;
private SingletoneExample()
{
}
public static SingletoneExample getInstance()
{
instance=new SingletoneExample();
return instance;
}
}
Step 5: From other class you call the singletoneExample class behaviour.
Step 6: The singletoneExample class becomes singletone.