What is Singleton’s design pattern?
Singleton design pattern is a part of the creational design pattern which is used to implement the creation of a single instance in an application (per JVM). This pattern is responsible to restricts the instantiation of a class to one object only. In JDK, java.lang.Runtime is an example of Singleton.
Using the Runtime class, we can get access to the Java runtime information like memory availability, invoking the garbage collector, etc.
Example: Sometimes in an application, we want the shared resource to have only a single instance like a database connection. So, a single instance of a database connection can be shared by all other instances of the application.
Creating multiple connection objects of the same type is not make sense and creation may be costly too. Objects are related to real-life examples. Handling the same kind of work can be done by a single object only.
In layman’s terms, a bank has many customers. For resolving the problems of every customer has a single front desk only. Not multiple dedicated front-desk services for every individual customer.
Applications of Singleton:
1. Caching: Cache as a singleton instance can be used in applications where we need the same kind of data multiple times and very frequently too. So having a single object of the class gives global access to all the future calls instead of frequent database calls.
2. Logger: Log files need to be generated and we required a logger object for this operation. So, the singleton pattern is a good example of a logger object. Logging is a very important aspect of every application. It helps you debugging your application in runtime.
3. Configuration: Another example, creating a singleton instance for the configuration file. Multiple instances read configuration files very frequently.
References:-
https://en.wikipedia.org/wiki/Singleton_pattern
https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html