Design patterns
singleton:
the singleton pattern is a design pattern that restricts the instantiation of a class to one object. It is used when managing access to shared resource. examples: a print spooler, manage a connection to a database. Lazy initialization:
public class SingletonDemo { private static SingletonDemo instance = null; private SingletonDemo() { }
public static synchronized SingletonDemo getInstance() {
if (instance == null) {
instance = new SingletonDemo();
}
return instance;
}
}
Eager initialization:
public class Singleton { private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}