Why we need Singleton Class?
6 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   venugopal_k
Posted On:   Monday, April 5, 2004 10:50 PM

Why we need Singleton class when we have a static type in java.
To have a single instantce of object across the application we are making use of Singleton. Can we not approach the same thing using static reference to the instantce.
Can you please clear me on this.

Thanks in advance.

Re: Why we need Singleton Class?

Posted By:   hemanth_jain  
Posted On:   Wednesday, April 28, 2004 05:39 AM

I personally feel, the same thing can be acheived through static reference of the instance. Implementing a singleton comes in handy when it comes to passing/sharing the object across different classes or methods. Singleton helps us implement some kind of a static class type (which java doesnt have except for inner classes being static). Also, i object to any kind of carelessness taking place if there are multiple coders, because thru the JVM there will be a single static reference of that object regardless of wherever its being used. Pls clarify, if i am wrong. Add light to my comments.

Re: Why we need Singleton Class?

Posted By:   Jay_G  
Posted On:   Monday, April 12, 2004 06:08 PM

1) Singletons allow their factory methods to control the concrete class that is used, relieving the client of the burden. Static methods are bound at compile time, meaning that every client call has to make the decision of which static to call. E.g.,




class PrefManager{
final PrefManager instance = isOsWindows
? new RegistryPrefManager()
: new FilePrefManager();
PrefManager getInstance(){
return instance;
}
void savePref...
}


If statics were used here, the decision of where to save prefs would be left to the client everytime a pref is saved. Then if new pref managers were added, all the client calls that use it need to be updated.



2) Singletons can be passed around like normal objects. See Collections.EMPTY_LIST.



3) Singletons ensure that a constructor is called. Statics that share data must all check that it has been initialized before they work on it.

Re: Why we need Singleton Class?

Posted By:   kendrick_wilson  
Posted On:   Wednesday, April 7, 2004 11:54 AM

It provides a way to ensure that a class can only have one instance.

Config object.
Logger object.

static is a programming construct that bind data a element to a class.

It does not prevent you from creating multiple objects. If you have a large program with more than two coders, it is needed. Careless programming can break the design.

Re: Why we need Singleton Class?

Posted By:   Bhaskar_Raja  
Posted On:   Tuesday, April 6, 2004 06:20 AM


My answer is



If you class doesnot contain private constructor, it is possible to create more than one class right? so that singleton has come. If you are using static also giving problem when you are suing that same for difference object it will give conflit. that is why make it single class using singleton by using private constructor. without that private construtor how can we create a unique object and unique object handle.

Re: Why we need Singleton Class?

Posted By:   Christopher_Koenigsberg  
Posted On:   Tuesday, April 6, 2004 05:57 AM

Usually a singleton IS a static reference, in the class.

Re: Why we need Singleton Class?

Posted By:   Dieter_Decavele  
Posted On:   Tuesday, April 6, 2004 03:35 AM

Say you want to save your data. You have developed different drivers for different systems and you only need one instance of the appropriate driver. Having static methods would do the trick but then your application has to know which class to load. So, you will have to hardcode everywhere which driverclass (XMLDriver, AccessDriver, MySQLDriver, FlatFileDriver, ...) you want to load. If you work with a manager which takes care of that for you, you only have to do PersistenceManager.getDriver ("xml"). Which will return a singleton instance of the appropriate class to work with.
About | Sitemap | Contact