The Artima Developer Community
Sponsored Link

Java Answers Forum
about singleton classes uses

4 replies on 1 page. Most recent reply: Jun 3, 2004 12:32 AM by Shashank D. Jha

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
sweth_murali

Posts: 3
Nickname: sweth
Registered: May, 2004

about singleton classes uses Posted: Jun 1, 2004 12:17 AM
Reply to this message Reply
Advertisement
can anyone tell in which scenario we are using singleton classes and what is the use of singleton classes.


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: about singleton classes uses Posted: Jun 2, 2004 3:41 AM
Reply to this message Reply
Anywhere, where u dont want multiple instance of ur class (or for that matter u want fixed maximum number of instances) of ur class, u go for Singleton class. properties and variables for such class can be shared only to one client.

Shashank D. Jha

Posts: 68
Nickname: shashankd
Registered: May, 2004

Re: about singleton classes uses Posted: Jun 2, 2004 8:07 AM
Reply to this message Reply
Singleton class ensures that at any instance of time there is only one object of its type exists.

Now the scenarios wherein you would like to use it where by any chance you don want to allow someone to create more then one instance, example

object of "President" of USA. (President is a class)

You will also use this pattern to control some specific behaiour that cannot be circumvented ex.

to ensure that every object within application share common time make Time class as singleton.

Singleton class also enables greated control on the way object may behave.


you may and will find several more reasons wherein you require singleton behaviour of a class.


hope this helps.

regards,
Shashank

Jai

Posts: 5
Nickname: jarulraj
Registered: Apr, 2004

Re: about singleton classes uses Posted: Jun 3, 2004 12:16 AM
Reply to this message Reply
What are the issues in having a singleton in the app server ? Like if two references happen simultaneously ?

Shashank D. Jha

Posts: 68
Nickname: shashankd
Registered: May, 2004

Re: about singleton classes uses Posted: Jun 3, 2004 12:32 AM
Reply to this message Reply
Well, it should not happen, unless singleton is not implemented properly.

Issues that may arise depends on the criticality of behaviour defined for that singleton class. Remr two presidents of USA!!

Or two threads now trying to change the state of data base objects (while data base objects were not thread safe) and were expecting co-operation from singleton class instance that were implementing thread safe feature.

So issues that may and can arise are immense depending on behaviour put within singleton class.

regards,
Shashank

Flat View: This topic has 4 replies on 1 page
Topic: Help me create these classes Previous Topic   Next Topic Topic: Journaling Application

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use