• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Singleton Pattern!!!!

 
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I would like to know an ideal scenario when we need to implement the singleton pattern...nay real life usage, or a case study where it is used
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The singleton pattern is used when one wants only one instance of an object in a system.
 
Rishi Singh
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's true pradeep but what could be the case when only instance of a class needs to be there.?
Iam not able to imagine this sort of scenario.
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is only one Sun for the whole universe.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, for example, using the Sun as a singleton, anytime an object needs to use the Sun object, they're all using the same Sun object, which more closely resembles the real world, anyway.
Corey
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rishi,
If we have to call the same class in so many places, then it is better to use singleton pattern.
Only disadvantage is that the system can't remove the static instance some time
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
One scenario where we can use the singleton pattern is in caching implementation. Where the clients access the singleton pattern (a single source) to get the cached data (which can be in a collection)
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One real example could be a School, were you have only one Director... The class Director (could extends Employee with others attributes) can use the Singleton Pattern to determine that only one Director will be instantiate...
I hope it helps...
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:
The singleton pattern is used when one wants only one instance of an object in a system.


This is a common misconception. It is a necessary but not a sufficient reason for wanting to use the Singleton pattern. If it is the only thing happening, code can become rather muddled through excessive use of Singleton. Often in an application you look at some class and say 'hmmm, I think there will only be one of these objects in my application, so I'll use Singleton'. Your application may represent an idiosyncratic use of the class; you could reduce reusability to no purpose by assuming that the *only* possible usage of the class is via a Singleton.
You use Singleton because:
1. there *must* be only one object
2. multiple sections of the code need to get access to that one object, but they won't know if it has already been created for them or not
3. the Singleton behaviour could vary (e.g. you could have subclasses of the Singleton, and have some factory-like abstraction hiding which one is actually used).
So bottom line: use Singleton when you want to share, manage the lifecycle of what is shared, and have the option of configuring the behaviour of what is shared. Don't simply use it because you think you'll only call 'new' once.
 
Reid M. Pinchback
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gustavo Adolpho Bonesso:
One real example could be a School, were you have only one Director... The class Director (could extends Employee with others attributes) can use the Singleton Pattern to determine that only one Director will be instantiate...


This is actually a perfect example of reduced reusability as a consequence of Singleton being used where it shouldn't. School 'has a' Director. That means School contains a member variable referencing a Director; no Singleton is needed here.
If you create these classes for academic organization 'A', and wanted them to be re-used by academic organization 'B', you could find that 'B' is an institution consisting of multiple things they call 'School' (yes, there are such places), each with its own 'Director'. Use of the Singleton pattern would force all of the schools to have the exact same Director!
The situation would be even worse with an example from the corporate world. In many organizations, a 'Department' would have a single director. A change in management style could result in a matrix-managed organization where you have 2 or more directors per department.
Singleton is at its best when it is managing access to some resource external to your program. Then you can't really "create" more of those resources in your code, you can only use what truly exists in physical reality. Examples might be the windowing system for your computer, a database connection that doesn't support concurrent access from multiple clients (older ODBC versions), etc.
[ June 07, 2002: Message edited by: Reid M. Pinchback ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic