• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

implementing singleton pattern

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
if i implement singleton pattern, i will be doing something like this

public class Singleton{
private static Singleton obj;

private Singleton(){}

public Singleton getInstance(){
if(obj == null) obj = new Singleton();
return obj;
}
}

this is fine, but i was asked one question
if i serialize the object and then desrialize it, what will happen and how can i make sure that when after desrializing on a different jvm, my contructor call should not happen whrn i require the instance..

i know in serialization we have to explicitly serialize static objects, but even in that case i'm not sure hw my constructor call will not happen, because at the most, what i ll do is to serialize the variables definig state of the object and then restoring that state.

i'm somewhat stuck .
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two things,
1) Your implementation of the singleton pattern although correct is not 100% guaranteed to produce one instance, there is a small chance that there could be more than one instance created. This version on the singleton is preferred.

public class Singleton {

public static final Singleton INSTANCE = new Singleton();
//private constructor
private Singleton() {
//do some construction
}

public Singleton getInstance() {
return INSTANCE;
}
}

//you can then access it by using
Singleton.INSTANCE;
or
Singleton.getInstance()
[/LIST]

Secondly if you wanted to serialize this class you would need to implement serialization, if you don't know much about serialization read this short article on Sun website
http://java.sun.com/developer/technicalArticles/Programming/serialization/
There is a lot more to serialization than people assume so make sure you fully understand serialization before just implementing. One example is the the virtual machine will add a public constructor to this singleton class!


If you correctly serialize this object the state of the object will be maintained so if you deserialize it the getInstance() would not call the constructor again.

Shawn
 
gaurav abbi
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Shawn,
can you tell me the case in which it can go wrong, if you are looking from multithreading prespective, i can synchronize the block of code which checks whether the obj is null or not...
any other flaw, please let me know
 
Poop goes in a willow feeder. Wipe with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic