"A class of which only a single instance can exist"
- Singletons can be lazy loaded. Only when it is actually needed. That's very handy if the initialization includes expensive resource loading or database connections.
- Singletons offer an actual object.
- Singletons can be extended into a factory. The object management behind the scenes is abstract so it's better maintainable and results in better code.
You can use it to create a connection pool. It’s not wise
to create a new connection every time a program needs to write something to a
database; instead, a connection or a set of connections that are already a pool
can be instantiated using the Singleton pattern.
For
example,
public class Singleton {
private static Singleton instance;
private
Singleton(){}
public static Singleton
getInstance() {
if
(instance == null)
instance = new
Singleton();
return
instance;
}
}
Comments
Post a Comment