What are the main uses of Singleton design pattern in Java project?

Some of the main uses of Singleton design pattern in Java are as follows:

  • Runtime: In JDK, java.lang.Runtime is a singleton-based class. There is only one instance of Runtime in an application. This is the only class that interfaces with the environment/machine in which Java process is running.
  • Enum: In Java, enum construct is also based on Singleton pattern. Enum values can be accessed globally in same way by all classes.
  • Properties: In an application it makes sense to keep only one copy of the properties that all classes can access. This can be achieved by making properties class Singleton so that every class gets same copy of properties.
  • Spring: In Spring framework, all the beans are by default Singleton per container. So there is only one instance of bean in a Spring IoC container. But Spring also provides options to make the scope of a bean prototype in a container.

Leave a Reply

Your email address will not be published. Required fields are marked *