The Phoenix
Posts: 6
Nickname: phoenixb
Registered: May, 2003
|
|
Re: Singleton vs Class with static methods
|
Posted: May 14, 2003 6:41 AM
|
|
> You can pass the instance to methods, so it isn't treated > as a global and users are not so tightly coupled to it, > especially if it implements an interface that could > potentially be implemented by other classes (singletons or > not). Things you do with objects will be simpler and > cleaner (serialization, comparison, aggregation, etc.). 1. What is the need for passing a reference to a Singleton? By definition, the only way to obtain a reference should be by calling a typical "getInstance()" method. Within a single JVM, we shouldn't be passing references of Singletons to methods. 2. If you are talking about passing a reference of a Singleton to a remote JVM, then your point makes sense although i don't see any motivation for that. Even assuming there is a need, the difference comes into play ONLY when there is modifiable state in the Singleton. A Singleton reference can be passed with its state intact whereas it doesn't make sense in passing a .Class object :)
> At some later time, if you decide to make it > multi-instance, that will be a relativly easy change to > implement. Agreed
> Singletons make sense when you think of the thing as a > object of which you only expect to have one instance. > Static methods make sense when you have a group of > f methods which are really all stand-alone, but can be > grouped in a class for clarity (like math or the like). > In this case the class may be a zero-ton and really only > y serves as a namespace or package. (This is aside from > the case of having a static helper method or two in a > class which can be instantiated). Maybe you have a point but i'm not convinced.
|
|