Singleton Design Pattern needs to be used when :
1) Application needs one, and only one, instance of an object
2) Provide a global point of access to the object
Here are the thumb rule for creating singleton class
1) Create private constroctor
- This is required so that no one can create instance directly using "new" keyword"
2) Create "static" method to return Singleton class's object (Lets say method is "getInstance()")
- This method is used to create and return object of "Singleton" class
- This method will make sure that only one object is available
- Since constructor is private in that case we can not create object and access method hence we need a static method which can be
accessed directly with the "Class" name
3) Create method "getInstance" as "synchronized"
- To avoid creating two objects accidently in case of multi threading environment
4) Create "private" data member of "Singleton" class
- So that datamember can't be accessed directly
Here is the code snippet for the quick reference:
class Singleton
{
private static Singleton instance;
private Singleton() {
//Private constructor, because we don't allow object to be created using new keyword
}
public static synchronized Singleton getInstance()
{
//Checks if object is not created then only it will go and create next time onwards it will return already created instance
//keeping this method synchronized is very important where multiple threads gets executed in parallel
if (instance == null)
instance = new Singleton();
return instance;
}
}
Now, you can access/create object of a "Singleton" class like this.
Singleton singleton = null;
singleton = Singleton.getInstance();
Note: Singleton design pattern ensures that one object is available per JVM, in case of clustering environement where more than one JVM are available at that time as many objects would be created as many JVMs.
Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics
1) Application needs one, and only one, instance of an object
2) Provide a global point of access to the object
Here are the thumb rule for creating singleton class
1) Create private constroctor
- This is required so that no one can create instance directly using "new" keyword"
2) Create "static" method to return Singleton class's object (Lets say method is "getInstance()")
- This method is used to create and return object of "Singleton" class
- This method will make sure that only one object is available
- Since constructor is private in that case we can not create object and access method hence we need a static method which can be
accessed directly with the "Class" name
3) Create method "getInstance" as "synchronized"
- To avoid creating two objects accidently in case of multi threading environment
4) Create "private" data member of "Singleton" class
- So that datamember can't be accessed directly
Here is the code snippet for the quick reference:
class Singleton
{
private static Singleton instance;
private Singleton() {
//Private constructor, because we don't allow object to be created using new keyword
}
public static synchronized Singleton getInstance()
{
//Checks if object is not created then only it will go and create next time onwards it will return already created instance
//keeping this method synchronized is very important where multiple threads gets executed in parallel
if (instance == null)
instance = new Singleton();
return instance;
}
}
Now, you can access/create object of a "Singleton" class like this.
Singleton singleton = null;
singleton = Singleton.getInstance();
Note: Singleton design pattern ensures that one object is available per JVM, in case of clustering environement where more than one JVM are available at that time as many objects would be created as many JVMs.
Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics
No comments:
Post a Comment