Sunday, November 19, 2006

Design Patterns



Singleton Design Pattern:
A Singleton is a class that can be instantiated only one time in a JVM. It ensures that only one instance of the class available and provides global point of access.

Example:


public class MySingleton {

private static MySingleton myInstance; //static instance

private MySingleton() {
} //private constructor

public static MySingleton getInstance() {
if(myInstance == null)
myInstance = new MySingleton(); //checking for exsisting instance
return myInstance;
}
}


In any class to use the above singleton class:

MySingleton singletonFrame = MySingleton.getInstance();

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home