Mastering Singleton Design Pattern in Java: A Comprehensive Guide
Singleton Pattern says that just "define a class that has only one instance and provides a global point of access to it".
In other words, a class must ensure that only single instance should be created and single object can be used by all other classes.
There are two forms of singleton design pattern
Early Instantiation:
Early instantiation in the Singleton design pattern refers to the approach where the single instance of the class is created eagerly, i.e., as soon as the class is loaded by the Java Virtual Machine (JVM) or at the time of class loading. This means that the intance is created regardless of whether it is immediately needed or not.
Early Instantiation Example
Early Instantiation |
in this example:
The instance variable is declared as private and final, making it a constant, ensuring that can be assigned only once.
The constructor of the class is made private to prevent external instantiation.
The instance is created and assigned at the time of class loading, thanks to the static block.
When to use Early Instantiation:
Predictable Usage: if it's highly likely that the instance will be needed as soon as the application starts.
Resource Efficiency: When the cost of creating the instance is relatively low, and the benefits of early instantiation outweigh the drawbacks.
Lazy Instantiation:
Lazy instantiation in the Singleton design pattern refers to the approach where the single instance of the class is created only when it is first requested. This is done to defer the instantiation process until it is absolutely necessary, potentially saving resources and improving the efficiency of the program.
Here's an example of lazy instantiation in the Singleton design pattern in Java:
Lazy Instantiation |
In this example:
The instance variable is initially 'null';
The getInstance method checks if the instance is null. if it is, a new instance is created; otherwise, the existing instance is return.
When to Use Lazy Instantiation:
Deferred Initialization: When the cost of creating the instance is relatively high, and you want to defer that cost until necessary.
Resource Considerations: If conserving resources and optimizing memory usage are critical.
Thread-Safe Lazy Instantiation:
The example provided above is not thread-safe. In a multithreaded environment, multiple threads might simultaneously check if instance is null and then proceed to create separate instances. To address this, you can introduce synchronization mechanisms like double-checked locking or use the synchronized keyword.
ThreadSafeLazyInstantiationSingleton Example |
The volatile keyword is used to ensure that the variable instance is properly shared among threads. Double-checked locking is one way to achieve thread safety while minimizing the performance impact.
Advantage of Singleton design pattern
Save memory because object is not created at each request. Only single instance is reused again and again.
Comments
Post a Comment