In the previous post I had said that it may not be a good idea to have static attributes in your class. The Singleton design pattern also uses a static attribute to hold the Singleton instance. Even though there are valid uses of Singletons, lately this pattern has come under considerable attack. Here is another page on the Portland Pattern Repository Wiki, that outlines a practical problem someone faced while using Singletons.
Google has an open source tool to detect Singletons. They call it the Google Singleton Detector. They have identified 4 types of Singletons, namely: Singletons, Hingletons, Mingletons, and Fingletons. You can find definitions for all the funny _ingletons on their wiki, but for those of you too lazy to visit the wiki, I will quote them here:
Singleton:
A class for which there should only be one instance in the entire system at any given time. This program detects singletons which enforce their own singularity, which means they keep one static instance of themselves and pass it around through a static getter method.
Hingleton:
Derived from “helper singleton,” a Hingleton is a class which turns another class into a singleton by enforcing that class's singularity. The offending class is the Hingleton, which "hingles" the other class.
Mingleton:
Derived from "method singleton,” a Mingleton is a class which has any static method that returns some state without taking any parameters. Many will consider these edge cases and not want to report on them.
Fingleton:
Derived from "field singleton,” a Fingleton is a class which contains static fields. Many will consider these edge cases and not want to report on them. The idea is to use this tool to identify and remove all the _ingletons from the code.
Here is a very good blog post by Misko Hevery explaining why Singletons should be avoided.
And here is a video by Misko Hevery on Global State and Singletons,
Subscribe to:
Post Comments (Atom)
1 comments:
Hi Parag,
Good To see you back to blogosphere!!
Nice Post!!
I am eagerly waiting for your next post!!
-Lalit.
Post a Comment