Tuesday, June 23, 2009

Singleton class Example

Singleton Class

Ensure that only one instance of a class is created
Provide a global point of access to the object
Allow multiple instances in the future without affecting a singleton class's clients

Example 1 shows a classic Singleton design pattern implementation:

Example 1. The classic singleton

public class ClassicSingleton {
private static ClassicSingleton instance = null;
protected ClassicSingleton() {
// Exists only to defeat instantiation.
public static ClassicSingleton getInstance() {
if(instance == null) {
instance = new ClassicSingleton();
return instance;

No comments:

Post a Comment

Your Ad Here ]]>