Sometimes it's required to have only one instance of an object and it will be available throughout the entire application, such types of classes in java are called Singleton classes, the object is called Singleton
object and the design pattern is called Singleton design pattern
.
Ensure a class only has one instance and provide a global access to it.
Singleton design pattern
is one of the Gangs of Four Design Patterns
and It falls under the category of the creational design pattern
. Singleton
classes control the object creation and resources. it makes sure that only one object of a class exists at any point in time per JVM instance
.
Unlike normal classes, a singleton class is not destroyed by the end of the application's life cycle
Java provides some inbuild Singleton
classes such as java.lang.Runtime
, the entire application required only one run time, hence it makes sense to have a singleton runtime object.
- java.lang.Runtime have getRuntime() method
- java.awt.Toolkit have getDefaultToolkit() method
- java.sql.DriverManager have getDrivers() method
- Java.awt.Desktop have getDesktop() method
Requirement of Singleton class
Singleton
class limits the number of object creations to one. It ensures resource and access control of the application or database.
For each object creation, JVM
has to allocate space in the heap, Singleton
classes allow only one object creation which saves memory.
For certain configuration settings, caching, and logging where everything should be governed at a centralized place, Singleton
objects are used.
A Singleton
class object is unique per JVM
, if distributed system or application is using the object then it might lead to issues. Java support different types of class loaders and every class loaded might create its own version of the Singleton
object.
Singleton Class Application on Web Srever
A singleton class can be run on a web server as well. The Tomcat web server uses a different class loader for each web app. Every time a class loader encounters a new class, it first checks if the class is present in the web app classpath, ie shared lib or JVM.
Two instances of an application having a singleton class running on the same web server will have 2 singleton objects at a time because both the singleton class are not present in the web app classpath, so the individual class loader of both applications will create a singleton instance for them.
Singleton object is unique per JVM or Runtime, so if 2 instances of the same application are using singleton object of JVM or Tomcat shared libs, eg Runtime then both will have the same object.
Difference between Normal Class and Singleton class
The Singleton
class and Normal
class in java are distinguished by the process they get initialized. Normal
classes instantiate using a constructor but Singleton
class are instantiated by the static method.
Normal
classes get destroyed by the Garbage Collector
or end of the application, while Singleton
classes remain in the JVM
.
Drawback of Singleton
Singleton
object once created is globally accessible, hence it cannot be mocked, each change in state will reflect everywhere and will make testing harder.- The same
Singleton
object will be used throughout the application, which makes it tightly coupled, a small change in the singleton class might take the application down - The unit testing tests module on the unit level and based on condition, every time a test case run, the same object should have a different value based on the test case, that's why static fields are avoided during the test case.
Singleton
object is used throughout the application, so state change from the previous test cases will be available for all test cases. - In a multi-thread testing environment, where each object should have its own thread-local value and independent of each other, a singleton is hard to manage.
Singleton objects
are created usingstatic methods
, and static methods are hard to mock.- Unlike
normal objects
,singleton objects
are notgarbage collected
.