• 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: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To my little knowledge, we can use singleton for logging and database connection. where else can we use this pattern?

we can have ONE AND ONLY ONE instance or variable no. of instances. How do we determine the no. of instances depending upon the scenario?
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To my little knowledge, we can use singleton for logging and database connection. where else can we use this pattern?


If you like, you might apply Singleton in Caching management and Context management as well. You might want to cache data that retrieved from the DB, also, you might want to cache some user info from the application context.


we can have ONE AND ONLY ONE instance or variable no. of instances. How do we determine the no. of instances depending upon the scenario?


For singleton, you only have 1 instance in the system, otherwise, you will need to perform synchronization of data between instances. In case you wanna have more instances, you might use pooling to locate the instances, and the pool could take care on how much instances it should have. Just like what you did in connection pooling.

Nick
[ December 02, 2004: Message edited by: Nicholas Cheung ]
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

we can use singleton for logging and database connection



you can use Template Methods and Strategy pattern for logging and use Flyweight for connection pooling of database connnection.
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Naina Si:
we can have ONE AND ONLY ONE instance or variable no. of instances. How do we determine the no. of instances depending upon the scenario?



Open up any connection pooling class & you'll get to know.
 
author
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Naina Si:
To my little knowledge, we can use singleton for logging and database connection. where else can we use this pattern?



You are better off, in most cases, not using the singleton pattern at all. Singletons are essentially global variables; use them too often and your system can turn into a rat's nest.
 
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 Michael Feathers:


You are better off, in most cases, not using the singleton pattern at all. Singletons are essentially global variables; use them too often and your system can turn into a rat's nest.



Kent Beck has a nice "description" of the Singleton pattern in "Test-Driven Development by Example":

(pg.179)
How do you provide global variables in languages without global variables? Don't. Your programs will thank you for taking the time to think about design instead.



I couldn't agree more...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic