I am looking for a situation where there is no alternative to singleton pattern.
Is there any possible such scenario? Please provide an example you practically faced.
I am looking for a situation where there is no alternative to singleton pattern.
Is there any possible such scenario? Please provide an example you practically faced.
TL;DR
There is no such thing as "no alternative" to a singleton.
Longer version
Singleton is a design pattern, and as such it helps solving a problem.
The problem a singleton is trying to solves is when you want to create only one instance of a class. Why would you want to restrict the number of instances ? there could be many reasons, to name a few:
So back to your question, Singleton, as a design-pattern (a tool), can be used when it's appropriate (like a hammer - you can use it to hit a nail, but you can always find an alternative, a stone for example, and use it to hit that same nail).
To sum up, there is no scenario where it's absolutely the only way to achieve something (without any alternative).
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. 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
Here is a simple way to try to understand it.
Have you heard of "global variables"? Singleton is an instance of a class that acts like a global variable. You don't want it instantiated multiple times. All accesses to it have to be to the same instance.