How can we make a class Singleton?
Page 1 of 1 • Share •
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;
}
}
Admin- Admin
- Posts : 131
Join date : 2011-08-16
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;
}
}
Admin- Admin
- Posts : 131
Join date : 2011-08-16
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;
}
}
Admin- Admin
- Posts : 131
Join date : 2011-08-16
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;
}
}
Admin- Admin
- Posts : 131
Join date : 2011-08-16

» My new Class Cards.
» HOW TO MAKE BOOKMARKS FROM METAL
» my dragon deck for kcvds any1 think it need help make a shoutout im fix it=]..
» Scones - how to make them
» Can someone help me make Griegers Deck from Yugioh 5D's
» HOW TO MAKE BOOKMARKS FROM METAL
» my dragon deck for kcvds any1 think it need help make a shoutout im fix it=]..
» Scones - how to make them
» Can someone help me make Griegers Deck from Yugioh 5D's
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum