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:
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:
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.