Archives: Design Patterns

Java – Singleton Class Implementation

Following implementation shows a classic Singleton design pattern:
[code lang=”java”]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;
}

}[/code]