Singleton pattern is one of the most commonly used patterns in Java. It is used to control the number of objects created by preventing external instantiation and modification. This concept can be generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects, such as:
- private constructor - no other class can instantiate a new object.
- private reference - no external modification.
- public static method is the only place that can get an object.
The Story for Singleton
Here is a simple use case. A country can have only one president. So whenever a president is needed, the only president should be returned instead of creating a new one. The
getPresident()
method will make sure there is always only one president created.
Class Diagram and Code
Eager Mode:
thePresident
is declared as final
, so it will always contain the same object reference.
Lazy Mode:
Singleton Pattern Used in Java Stand Library
java.lang.Runtime#getRuntime()
is a frequently used method from Java standard library.getRunTime()
returns the runtime object associated with the current Java application.
Here is a simple example of using
getRunTime()
. It reads a webpage on a Windows system.
Output:
Pinging programcreek.com [198.71.49.96] with 32 bytes of data: Reply from 198.71.49.96: bytes=32 time=53ms TTL=47 Reply from 198.71.49.96: bytes=32 time=53ms TTL=47 Reply from 198.71.49.96: bytes=32 time=52ms TTL=47 Reply from 198.71.49.96: bytes=32 time=53ms TTL=47 Ping statistics for 198.71.49.96: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 52ms, Maximum = 53ms, Average = 52ms
Another Implementation of Singleton Pattern
As private constructor doesn't protect from instantiation via reflection, Joshua Bloch (Effective Java) proposes a better implementation of Singleton. If you are not familiar with Enum, here is a good example from Oracle.
* Thanks to Dan's comment.