Quote:Original post by Hodgman
I am legitimately justified in applying the Singleton or Monostate pattern here? Or am I just imagining the constant emergence of this need in my work?
The checklist in this
article on Singletons suggests that a singleton would be justified if the following conditions are true:
- Is there a need for the considered functionality to be grouped in an object?
- Is there a need for the considered functionality to be grouped in a class?
- Is there a technical reason for which the considered functionality does not allow multiple instances of the class?
- Is there a need for the unique instance to be globally accessible?
Have a read of the article for a more in-depth look at each of those items and the reasoning behind it. Your description doesn't go into a great deal of detail, but the requirement for global access may not be neccesary in your example.
Quote:Original post by argonaut
In essence, if you are to explain design patterns at all, you must first explain the Singleton.
There's absolutely no reason you couldn't teach the idea of design patterns without mentioning Singletons at all, nor are singletons neccesarily a best first place to start if you're teaching specific design patterns rather than just introducing the concept. They may well be a valid option as a first pattern to teach or relatively simple example of the idea of a design pattern (I'd personally avoid them, as I consider them to be very overused and often poorly understood, and therefore may be better left till the programmer is a bit more experienced), but "may first explain the Singleton" is very different from "
must first explain the Singleton" as you suggested.
Quote:Agreed, but is the pattern bad or the teaching bad? I use singletons once in awhile, does that make me evil or the reason I use them evil?
The abuse of the Singleton pattern is of course due to misunderstanding and misapplication, which may be a result of poor education on the subject. The pattern itself is certainly valid in some (I would suggest rare) cases, and generally when a capable programmer refers to a construct as "
evil" they do understand that and are rather indicating that due to common overuse and/or misunderstanding people (particularly those who may be knew to the construct or pattern in question) should take additional care that their usage is indeed appropriate.
We know the pattern is appropriate and useful
sometimes, but are suggesting that those cases are rarer than a lot of people think and that more thought should be put into possible alternatives before jumping in with the pattern. Learning about Singletons is
not a requirement for understanding the underlying concept of design patterns, nor is knowledge of it prerequisit for learning most other design patterns, so it need not neccesarily be the first pattern someone is taught.
[Edited by - jbadams on May 14, 2008 3:40:06 AM]