Singleton pattern vs static methods... which is better?
Posted By:
Eric_Lindauer
Posted On:
Wednesday, August 14, 2002 09:02 PM
These are excellent points about the differences between static methods and singletons. I would go farther though and argue that the advantage of polymorphism in the returned type makes the singleton pattern superior to the static method design.
One of the main goals of OO programming is to make objects and packages which are open to extension while being closed to modification. When you choose to make your objects all static methods, you have effectively cut off any future attempts to extend your objects without modifying your source code. However, with the singleton pattern, it is often a trivial matter to have a dynamic way to determine the type of class to instantiate ( a Properties objects or some other type of runtime preference comes to mind ), and in this way, other users of your code can plugin their own implementation, and all the code you've already written that uses the singleton continues to work, but in the new way that the third-party requires.
This is, in my opinion, one of the fundamental criteria of a good design (closed to mods, open to extension). As such, I'd recommend that you always choose the singleton pattern over the class-full-of-static-methods.
Hope this helps.
Eric