aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes How to get the Single Instance of a class - singleton -Basic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "How to get the Single Instance of a class - singleton -Basic" Watch "How to get the Single Instance of a class - singleton -Basic" New topic
Author

How to get the Single Instance of a class - singleton -Basic

kalpana Kumar
Ranch Hand

Joined: Jul 03, 2003
Posts: 65
Hi all,
I have a basic question as to how to obtain a single instance of a class other than using Singleton Pattern.

I really appreciate any suggestion.

Thanks
Logan Owen
Greenhorn

Joined: Dec 01, 2004
Posts: 24
Do you mean create a new instance? Because that would be:



Or whatever the constructor is.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
Moving this to the OO, Patterns, UML and Refactoring forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.

An alternative is discussed at http://butunclebob.com/ArticleS.UncleBob.SingletonVsJustCreateOne


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
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://aspose.com/file-tools
 
subject: How to get the Single Instance of a class - singleton -Basic