posted 20 years ago
A singleton pattern limits the number of instances that can be created for a particular class. So -- by definition -- whatever code you use to achieve this is a "singleton pattern."
In general, the constructor is made private, and then a static method keeps track of how many instances have been created, and returns a new instance if appropriate.
There are variations on how to implement this. For example, if the singleton is actually meant to limit the number instances to one, then it could be as simple as making the constructor private, declaring a private static instance of the class as a member, then using a public static method to return a reference to that static instance.
Other approaches get more complex -- using counters to track the number of instances, or cleverly employing "this" and "null." Try searching the boards (or the net) for "singleton."
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org