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
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
posted
0
Moving this to the OO, Patterns, UML and Refactoring forum...
Originally posted by marc weber: 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."
The global access point (the getInstance() method) also is inherent to the Singleton pattern.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
The GoF book has a section on subclassing and accessing singletons, talks about factories, registries and such. Then the class itself doesnt guarantee that only a single instance is every made and doesn't provide the single point of access. Other objects do that for it.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: How to get the Single Instance of a class - singleton -Basic