Where exactly the Singleton Pattern is used in real application?


Java Problem Overview

I was just curious where exactly the singleton pattern is used... I know how the pattern works and where it can be used but i personally never used in any real application. Can some one give an example where it can be used.. I would really appreciate if some one can explain how and where they have used in real application. Thanks, Swati

Java Solutions

Solution 1 - Java

Typically singletons are used for global configuration. The simplest example would be LogManager - there's a static LogManager.getLogManager() method, and a single global instance is used.

In fact this isn't a "true" singleton as you can derive your own class from LogManager and create extra instances that way - but it's typically used as a singleton.

Another example would be java.lang.Runtime - from the docs:

> Every Java application has a single > instance of class Runtime that allows > the application to interface with the > environment in which the application > is running. The current runtime can be > obtained from the getRuntime method.

That's pretty much the definition of a singleton :)

Now the singleton pattern is mostly frowned upon these days - it introduces tight coupling, and makes things which use the singleton harder to test, as you can't easily mock out that component. If you can get away without it, so much the better. Inject your dependencies where possible instead.

Solution 2 - Java

Some examples:

  • Hardware access
  • Database connections
  • Config files

Solution 3 - Java

I used the singleton pattern in an online Football Team Store System. we applied the singleton pattern to a ShoppingCart class.

You only needed one instance of the cart per an application instance. so the singleton seemed like it's the best fit for that situation.

Solution 4 - Java

Consider the situation of AudioDriverService which is designed in Singleton Pattern. So we are allowed to create just a single instance of AudioDriverService class. Now actually your Windows Media Player or Youtube Player both will share the same object of AudioDriverService rather than creating a new instance.

Solution 5 - Java

When you use Logger, you use Singleton pattern for the Logger class. In case it is not Singleton, every client will have its own Logger object and there will be concurrent access on the Logger instance in Multithreaded environment, and multiple clients will create/write to the Log file concurrently, this leads to data corruption.

Solution 6 - Java

For example running a trial version of a software with one license and one database connection ,that uses singleton pattern in real word. may be the guru jon skeet can provide example like this.

Solution 7 - Java

Singleton pattern is generally useful when the object that is created once and shared across different threads/Applications. Consider the case that you are implementing the properties load class for a printer.

Now Printers can be of variant properties. For eg:- Mono Printer, Color Printer, Automatic Scanner Support Printer and so on...

Every time on boot this config file has to load to enable a few buttons/ applications on the UI say tab or any Printer UI.

The value of the supported features are stored in form of a config table like say 1 if feature supported and 0 if not supported.

Based on the supported features we enable disable certain functionalities and application on the UI. Now this config file location in case of printers manufactured by a single company are always stored at a fixed path.

The file values would change/would be read only in the following scenarios:-

  1. On Boot.
  2. on Addition or deletion of any new hardware peripheral.

We can use a singleton class to implement the reading of configuration files. As the same values i.e. the config does not change on UI intervention and can be changed only on hardware intervention.

This is one example I can think of where we can implement Singleton design pattern.

Solution 8 - Java

Singleton is a nice design pattern. Before deciding on the pattern first do an in depth analysis of your problem and the solution. If in your solution some object has only one instance and you want to model that in your design then you should use singleton pattern. For example if you are modelling a PC in the software there can be only one instance of a PC with respect to your running program. As Jon Skeet said java.lang.Runtime is modelled as a singleton because for all the java objects that are loaded and running inside a java runtime there is only one instance of runtime.

Again lot of times it is used for the wrong reasons. Never create a singleton so that you can easily access the object (like Object::instance() ) from anywhere without passing the object around. The is the worst use I have ever come across.

Solution 9 - Java

I use a media player in my android app, so I extend the mediaplayer class, and used the singleton pattern because I need only one instance, and be able to call the same instance in any other part of my app for check if is playing, or what file is current playing.

Hope it helps you, regards!!!!

Solution 10 - Java

Singleton Pattern can be used for loading the configuration , say in a web application, we are supposed to generate a file which is to be stored somewhere in the server. Now, that location can fetched from a config file(properties file) using a singleton class.since the location is to be unique and might change frequently in future, so we use a config file so as can be modified without deploying the application so as to reflect the changes and location will be global and unique through out the application

Solution 11 - Java

I used a singleton (actually a couple of them) in an application that used pureMVC. We were unhappy about the complexity this framework introduced (it became complicated and tiering to track method calls through the mvc layers). So we used a central singleton as a mediator to better separate the layers from each other.

Solution 12 - Java

Singleton Class is basically used when you have to allow only single instantiation of the class. Taking real world example, In case of OOP designing of a library, we can create library class as singleton class.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionswatiView Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavaFlorianView Answer on Stackoverflow
Solution 3 - JavaManaf Abu.RousView Answer on Stackoverflow
Solution 4 - JavaMr. DView Answer on Stackoverflow
Solution 5 - JavaArun RaajView Answer on Stackoverflow
Solution 6 - JavaDead ProgrammerView Answer on Stackoverflow
Solution 7 - JavanetbloggerView Answer on Stackoverflow
Solution 8 - JavaferosekhanjView Answer on Stackoverflow
Solution 9 - JavaFranco Omar Castillo BelloView Answer on Stackoverflow
Solution 10 - JavaAkhil GuptaView Answer on Stackoverflow
Solution 11 - JavaMax LeskeView Answer on Stackoverflow
Solution 12 - JavaVishal KumarView Answer on Stackoverflow