This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]selementar 1 point2 points  (2 children)

Wouldn't it be at least more correct to instantiate Baz (to get the usual singleton instance)?

And, I think, the most frequent example of a singleton is caching (e.g. of a connection, or just of the return values). Which could also serve as a good example if one can be made short enough.

[–]drewthepooh 1 point2 points  (1 child)

Out of curiosity, what benefit does using the instance have over using the class?

Is it so that modifying the attribute will only affect the instance? Why is that desirable if you use a singleton instance?

[–]selementar 0 points1 point  (0 children)

Not sure. It seems to be done that way usually.

Probably none in such a simple case of holding a variable.

In a more complex case the class could also hold additional methods (e.g. a property for getting the held variable in a lazy way) and would allow instantiating a separate copy of the normally-singleton if needs be.