How can we make a class Singleton?
Page 1 of 1
How can we make a class Singleton?
A) If the class is Serializable
class Singleton implements Serializable
{
private static Singleton instance;
private Singleton() { }
public static synchronized Singleton getInstance()
{
if (instance == null)
instance = new Singleton();
return instance;
}
/**
If the singleton implements Serializable, then this
* method must be supplied.
*/
protected Object readResolve() {
return instance;
}
/**
This method avoids the object fro being cloned
*/
public Object clone() {
throws CloneNotSupportedException ;
//return instance;
}
}
B) If the class is NOT Serializable
class Singleton
{
private static Singleton instance;
private Singleton() { }
public static synchronized Singleton getInstance()
{
if (instance == null)
instance = new Singleton();
return instance;
}
/**
This method avoids the object from being cloned
**/
public Object clone() {
throws CloneNotSupportedException ;
//return instance;
}
}
class Singleton implements Serializable
{
private static Singleton instance;
private Singleton() { }
public static synchronized Singleton getInstance()
{
if (instance == null)
instance = new Singleton();
return instance;
}
/**
If the singleton implements Serializable, then this
* method must be supplied.
*/
protected Object readResolve() {
return instance;
}
/**
This method avoids the object fro being cloned
*/
public Object clone() {
throws CloneNotSupportedException ;
//return instance;
}
}
B) If the class is NOT Serializable
class Singleton
{
private static Singleton instance;
private Singleton() { }
public static synchronized Singleton getInstance()
{
if (instance == null)
instance = new Singleton();
return instance;
}
/**
This method avoids the object from being cloned
**/
public Object clone() {
throws CloneNotSupportedException ;
//return instance;
}
}
Re: How can we make a class Singleton?
class Singleton{
private static Singleton instance;
private Singleton();
public static synchronized Singleton getInstance(){
if(instance==null)
instance=new Singleton();
return instance;
}
}
private static Singleton instance;
private Singleton();
public static synchronized Singleton getInstance(){
if(instance==null)
instance=new Singleton();
return instance;
}
}
Re: How can we make a class Singleton?
Singleton to handle Multi-threading:
class Singleton{
private static Singleton instance;
private Singleton(){}
public static Singleton getInstance(){
if(instance==null){
synchronized (Singleton.class){
if(instance==null){
instance=new Singleton();
}
}
}
return instance;
}
}
class Singleton{
private static Singleton instance;
private Singleton(){}
public static Singleton getInstance(){
if(instance==null){
synchronized (Singleton.class){
if(instance==null){
instance=new Singleton();
}
}
}
return instance;
}
}
Re: How can we make a class Singleton?
Singleton to handle Multi-threading:
1 way: make the method synochronized (but will influence performance)
2 way: double check locking
class Singleton{
private static Singleton instance;
private Singleton(){}
public static Singleton getInstance(){
if(instance==null){
synchronized (Singleton.class){
if(instance==null){
instance=new Singleton();
}
}
}
return instance;
}
}
1 way: make the method synochronized (but will influence performance)
2 way: double check locking
class Singleton{
private static Singleton instance;
private Singleton(){}
public static Singleton getInstance(){
if(instance==null){
synchronized (Singleton.class){
if(instance==null){
instance=new Singleton();
}
}
}
return instance;
}
}
Similar topics
» Difference between Interface and Abstract Class?
» What is friend class?
» What is the difference between a struct and a class?
» What methods does Class Object has?
» What does it mean that a method or class is abstract?
» What is friend class?
» What is the difference between a struct and a class?
» What methods does Class Object has?
» What does it mean that a method or class is abstract?
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum