-1

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.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • 1
    Possible duplicate of [On Design Patterns: When to use the Singleton?](http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton) – Nir Alfasi Mar 09 '17 at 06:17
  • I wanted to know the situation where you practically used the pattern? this link doesn't answer this. – Shakhawat Hossain Mar 09 '17 at 06:25
  • 2
    I think the link does answer it to some extend, e.g. for logging you can use a singleton, or for some kind of application wide service object (like a database connection). – xander Mar 09 '17 at 07:01
  • I usually follow Singleton pattern to create JDBC Connection in my Java projects. – Mehmood Memon Mar 09 '17 at 07:02
  • what will be the problem? if you don't use singleton pattern for jdbc connection or logging.. – Shakhawat Hossain Mar 09 '17 at 07:09
  • I ask for a situation where there is no alternative to singleton pattern? – Shakhawat Hossain Mar 09 '17 at 07:10
  • it depends on the context, but a simple answer might be to keep the file/connection open in the single class object without using static class members for states or whatnot, there is always an alternative for a singleton pattern so you don't have to use it, but it makes some things easier and better to maintain. – xander Mar 09 '17 at 07:11
  • I wanna exactly know the context where there is no alternative to singleton. Is there any possible scenario ? – Shakhawat Hossain Mar 09 '17 at 07:13
  • 1
    **Single**ton, meaning you will want one, and only one, instance of some variable. Think of it like an application cache. – OneCricketeer Mar 09 '17 at 07:13

3 Answers3

2

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:

  1. Each instance is very expensive to create/maintain and we want to save resources
  2. We use the same resource from different places in the application and we want a simple mechanism to handle it without getting into complex solutions (using synchronization, for example).
  3. Creating more than one object would be not only expensive (#1) but also confusing and difficult to maintain, e.g. connection-pool

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).

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

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

Mirza715
  • 420
  • 5
  • 15
  • but I want to know, where we must have to use singleton design pattern? – Shakhawat Hossain Mar 09 '17 at 07:20
  • where you have to use only single instance of class.In that case it would be efficient and good to use Singleton. – Mirza715 Mar 09 '17 at 07:45
  • @MSHossain you may take help from this https://www.codeproject.com/Tips/219559/Simple-Singleton-Pattern-in-Csharp – Mirza715 Mar 09 '17 at 07:49
  • You missed the question, the OP asks for a situation where the **only solution** is using a singleton. He's asking for such a scenario. What you're giving him is an explanation of *what is a singleton* – Nir Alfasi Mar 09 '17 at 08:36
  • The OP clarifies it in the comments: "I wanna exactly know the context where there is no alternative to singleton. Is there any possible scenario ?" – Nir Alfasi Mar 09 '17 at 08:37
  • @alfasin there are many design patterns and there is not any real World example in my info which is only restricted to Singleton.You can chose design pattern where it is suitable. – Mirza715 Mar 09 '17 at 08:46
  • Exactly! And that's what I wrote as an answer ;) – Nir Alfasi Mar 09 '17 at 08:49
0

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.

sureshvv
  • 4,234
  • 1
  • 26
  • 32