Implement a Singleton

Go down

Implement a Singleton

Post  Admin on Thu Aug 25, 2011 8:49 pm

1. Define a private static attribute in the “single instance” class.
2. Define a public static accessor function in the class.
3. Do “lazy initialization” (creation on first use) in the accessor function.
4. Define all constructors to be protected or private.
5. Clients may only use the accessor function to manipulate the Singleton.

public class Singleton {
private static final Singleton INSTANCE = new Singleton();

// Private constructor prevents instantiation from other classes
private Singleton() {}

public static Singleton getInstance() {
return INSTANCE;


Posts : 131
Join date : 2011-08-16

View user profile

Back to top Go down

Back to top

Permissions in this forum:
You cannot reply to topics in this forum