Singleton Pattern
Lazy initialization
Use lazy initialization when you want a Singleton instance to be created on demand.
This way you are not creating an instance (say if the instance creation logic is time consuming) if it is not needed.
Lazy Initialization + multi-threaded scenarios
Approach 1
Use the following if getInstance() method isn't causing substantial overhead for the application.
So, if a high traffic part of the code begins using getInstance(), you may reconsider, and use approach 2.
Synchronizing a method can decrease performance by a factor of 100.
No comments:
Post a Comment