In most programming languages, classes elude the type system. Although a class with its static methods and variables is an object, it very often cannot implement an interface or extend other classes. For this reason, it cannot be used in a polymorphic manner, since it cannot be a subtype of another type. For example, if you have an IFooable interface that is required for several method signatures of other classes, a StaticFoo object cannot be used instead of IFooable , whereas FooSingleton.getInstance() can (suppose FooSingleton implements IFooable ).
Note that since I commented on Heinzi's answer, the singleton is a template for instance control. It replaces new Class() with Class.getInstance() , which gives the Class author more control over instances, which he can use to prevent the creation of unnecessary instances. Singleton is just a special case of the factory pattern and should be considered as such. General use makes it more likely a special case of global registries, which often ends badly because global registries should not be used perforce.
If you plan to provide global helper functions, then static methods will work just fine. A class will not act as a class, but rather as a namespace. I suggest that you remain highly cohesive, or you may encounter the strangest communication problems.
Greetz
back2dos
back2dos May 04 '10 at 12:34 a.m. 2010-05-04 12:34
source share