0

Not quite sure whether this has been asked before.

Language is c++. I am writing a class with a number of data items that should have a static behavior, say a buffer of 1 KB that holds some data and should be shared for all objects. My boss ask me, why don't you write a singleton class and put all the data items inside it?

I have some bitter experiences with singleton and also recently I have googled "singleton is evil" and read a couple of articles. In SO also I read the same. So I was a little hesitant to use singleton in the above case, because I believe it is not really required there. So I asked my boss, will it be OK to have static data members for the required data items, instead of making the class singleton? But he is not giving me any clear answer and tells me, design patterns are there to make life easier, so what is the problem in using it. I don't want to argue with him. :)

I am not an OOP expert. So it will be helpful if somebody can comment on whose point make more sense [also whether both are wrong :)].

Thanks...

7
  • It is hard to tell whether it would be useful, or what a better option might be in your case without you posting an example of the class you are using (and perhaps some more detail on what is to be achieved). Post it and we will take a look. Generally, Singleton, when used correctly can be a very powerfull and useful construct (IMO). Commented Jan 31, 2013 at 12:09
  • 1
    Singleton's are evil, goto's are evil, there are myriad programming constructs which are considered evil. Of course none of them were though to be evil when initially created, because they were used (by their creator) properly. "Evil" in this context really means "difficult to use if you're not careful / know what you're doing". So if you know what you're doing and are careful and they make sense, use them (and document very liberally for the next coder!). However, since you're not confident due to your past experience with singletons, they might not really be for you. Commented Jan 31, 2013 at 12:11
  • @Killercam : Thanks, the class is yet to be written, so unable to post it here:( .The only thing with this class is that most of the members variables are static. Commented Jan 31, 2013 at 12:13
  • 1
    @mah: Thanks. So the idea that singleton is the most easy pattern to understand is not valid, I think :). Commented Jan 31, 2013 at 12:16
  • No, Singleton is definitely not easy to understand. The implementation is dead simple, but the “when and how” is absolutely not. Commented Jan 31, 2013 at 12:20

1 Answer 1

2

A singleton is essentially nothing but some OO encapsulation around a global variable.

If you encapsulate your state in some class and just add that as a static member to the class that needs the info, you're not really doing much different from a singleton, just with at least two drawbacks:

  • You preclude sharing that state with other classes.
  • In the simple implementation, there's nothing to stop anyone from creating another instance of your new class. If that is semantically an invalid thing, you should not allow it in the code, and that means you have to write all that singleton functionality anyway.

Sure, Just slapping a bit of Singleton Magic on top of globals doesn't make the problems with global state go away, and in that sense, singletons are evil™. But there are cases where your model needs to have global state. Encapsulation is a good thing in those instances, since it allows you to control who modifies what, when, and how. You can make the singleton factory method only accessible to certain classes, for example.

Sign up to request clarification or add additional context in comments.

1 Comment

:Thank you for the insights. In my case multiple instances are not a problem [comment on you second point].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.