• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean create a new instance? Because that would be:



Or whatever the constructor is.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the OO, Patterns, UML and Refactoring forum...
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic