View RSS Feed

Design Patterns

Serialization

Rate this Entry
by , 03-09-2012 at 06:50 PM (580 Views)
If the java.io.Serializable interface is being implemented by the Singleton class ( when serialization and deserialization of singleton takes place more than one time) then number of Singleton instances will be created. For avoiding this, implement the readResolve method.
Java Code:
	public class Singleton implements Serializable {
		...

		// This method is called immediately after an object of this class is deserialized.
		// This method returns the singleton instance.
		protected Object readResolve() {
			return getInstance();
		}
	}

Submit "Serialization" to Facebook Submit "Serialization" to Digg Submit "Serialization" to del.icio.us Submit "Serialization" to StumbleUpon Submit "Serialization" to Google

Categories
Singleton Pattern

Comments