2

Well i read singleton are bad because they are anti pattern. I also read that the main reason for this is the global reference to singleton, anyway:

is it always possible to avoid singleton?

If so, lets says for example i got an IOCP network and i need to initialize it once and this object needs to be constant through the entire lifetime of the software. Same comes with a class i have called "paint" where i print data into the screen. If i didn't make a singleton of it i would still need a global variable of the current Hwnd and to initialize the object locally each time i am going to use it (really annoying).

So using singleton is a sign my design is flaw? What can i do to avoid them?

Thanks.

Pie_Jesu
  • 1,894
  • 3
  • 16
  • 30
ffenix
  • 543
  • 1
  • 5
  • 22
  • 3
    singleton is NOT an anti-pattern. It is a pattern! [Singleton@Wiki](http://en.wikipedia.org/wiki/Singleton_pattern). Also you can describe a getter method, which returns reference to instance in a class, and provide access through inheritance or aggregation. They are good, and using them isn't a mauvais ton. – Pie_Jesu Sep 20 '12 at 06:15
  • 2
    @Pie_Jesu You are so wrong. In 99.5% cases of singleton usage is wrong. – BЈовић Sep 20 '12 at 06:29
  • IMHO most people misunderstand singleton, its main focus is on not allowing the construction of other instances of the same class and as a consequence of that you have only one instance. If you take away the "you cannot instantiate another object of this type" you'll see that every global can be used as an alternative of a singleton. You can also just pass the object you need in the constructor of your class. – RedX Sep 20 '12 at 06:34
  • 1
    +1 for `i read singleton are bad`. True story. – ApprenticeHacker Sep 20 '12 at 06:37
  • I don't understand why `having 1 instance is bad` too. Its a good pattern and it should be use. Don't see good arguments to not using it. – Pie_Jesu Sep 20 '12 at 07:11
  • @Pie_Jesu: It is not wrong to have just one instance of a class. What is wrong is using the singleton pattern to achieve this. – wilx Sep 20 '12 at 07:53
  • @wilx you did misunderstood me. I prefer to use singletones too! – Pie_Jesu Sep 20 '12 at 08:19
  • @Pie_Jesu: No, I understand very well. I do not like the singleton pattern. As I have said, the singleton *pattern* is the problem, not that you want a single instance of a variable. There is a difference between the two statements. – wilx Sep 20 '12 at 08:55
  • Alternatives to the singleton pattern : http://programmers.stackexchange.com/questions/147698/alternatives-to-the-singleton-pattern , singleton vs. global static object: http://stackoverflow.com/questions/1463707/c-singleton-vs-global-static-object – Software_Designer Sep 20 '12 at 10:21

2 Answers2

5

is it always possible to avoid singleton?

Yes, use a global variable, or (even better) fix your design. One option to fix a design is to use some kind of inversion of control.

If you try to use OO principles, you'll see you can do without a singleton.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • I don't see how i can make an IOCP network without the use of singleton, i just need one instance of that object. – ffenix Sep 20 '12 at 06:38
  • 1
    @ffenix By creating only one instance, and passing it to constructors that needs it. That's one way – BЈовић Sep 20 '12 at 06:44
0

Its a question of which entities need access to the resource that can only be instantiated once, and when (henceforth called the resource).

If the entities that need access to this resource can be instantiated with the resource (IOC, dependency injection), then that is the best way to go, thus keeping things simple and avoiding creating a Singleton. KISS.

If, for some reason, there are entities that need access to the resource, but cant be instantiated with it, then an alternative needs to be implemented. One option is a Singleton, but another option that I like to use is a Factory. This completely encapsulates the creation of the resource, and is much more future-proof, meaning that if in the future for some reason, more than one instance of the resource can be instantiated, then its all encapsulated. You cant/shouldnt try to do this with a Singleton. Of course, internally the factory will maintain the unique instance of the resource.

There are those that would argue that if an entity cant be instantiated with the resource, then the design is bad. That can be argued, and should probably be done so an a case-by-case basis.

Brady
  • 10,207
  • 2
  • 20
  • 59