I am just quoting my opinion someone elses article. The actual article can be found at:
singletons-are-pathological-liars
Why is global bad? Yeah, I know what your are thinking, and no, I am not joking. There are certain languages like JS which are quite powerful and yet have limited ability to comeup with walls (modularity).
Global is bad because:
a. It causes namespace conflict
b. It exposes the state in a unwarranted fashion
When it comes to Singletons
a. The explicit OO way of calling them, prevents the conflicts, so point a. is not an issue
b. Singletons without state are (like factories) are not a problem. Singletons with state can again fall in two categories, those which are immutable or write once and read many (config/property files). These are not bad. Mutable Singletons, which are kind of reference holders are the ones which you are speaking of.
Now if they are designed to act as application cache, you may see a lack of adequate cohesion. So what do we do! Using the pathelogical lies analogy, hide the unpleasant details, in private methods or in interfacing Adaptors (IOC). Make sure that these kinds of singletons are restricted to above sections of code and for the rest of application they become person-nongrata. Finally when you test the application, you won’t see the singletons. Not fool proof, but just makes it a wee bit difficult to accidentally modify things.
If we are exposing state of instance to Singletons in any other way, it is a bad OO design.
Quoting Dalai Lama, “Know your rules well, so that you can break them”.
Reality is a perception. Perceptions are not always based on facts, and are strongly influenced by illusions. Inquisitiveness is hence indispensable
Thursday, September 25, 2008
Subscribe to:
Post Comments (Atom)
Popular Posts
-
I recently had to come with this data-structure, later I found that google collections has a MapMaker which essentially does the same. Post...
-
This is the way I like to handle events. Note the ease with which the MessageSenders and MessageListeners can be "weaved" using ao...
-
There are times when we face the need to marshall and unmarshall java objects. What better than XML for this! Most programmers can write the...
-
Bananas for the code monkey It is always a good idea to prevent users from doing unwarranted things. Thats the whole idea of client side val...
-
Event bus is a rather simple notion, that is of great aid. Think of a telephone network; to communicate between two ends, one would require ...
Labels
- Programing (13)
- monologues (8)
- Java (7)
- experiences (7)
- ideas (2)
- java script (2)
- CSS (1)
- GXT (1)
- My First Post (1)
- Politics (1)
- movies (1)
About Me
- Swaroop
- Well for a start, I dont' want to!. Yes I am reclusive, no I am not secretive; Candid? Yes; Aspergers? No :). My friends call me an enthusiast, my boss calls me purist, I call myself an explorer, to summarise; just an inquisitive child who didnt'learn to take things for granted. For the sake of living, I work as a S/W engineer. If you dont' know what it means, turn back right now.
No comments:
Post a Comment