Re: Singletons
- From: Robert Martin <unclebob@xxxxxxxxxxxxxxxx>
- Date: Thu, 20 Jul 2006 17:46:33 -0500
On 2006-07-19 23:46:42 -0500, raxitsheth2000@xxxxxxxxxxx said:
There are some good uses for Singleton; specifically in frameworks that
will be used by many other people over whom you have little control.
Singleton can enforce a constraint that programmers might otherwise
violate. However, in close-knit development teams the enforcement is
uncessary since you can *talk* to each other and rely on convention.
When one *talk* with developers/collegue/testers, it would be
responsibility of them (team) to ensure that any time only one
instance of the class would be there,
what happens when someone break that condition. may be scrap....!!!
Yes, there may be some scrap. Scrap is not something to fear. Indeed, scrap is a necessary part of writing systems. Scrap is not waste. Scrap means improvement.
We dont' write papers, stories, novels, music, or anything else creative, without continuous review and change. We are constantly writing first, second, third, and more drafts. The act of creation is an act of incremental and successive improvement. So it is with software. No system will ever be written "right" the first time. All successful systems are built through incremental improvement.
When one implement class as Singleton, Now the class itself is
responsible to take care that No more than one instance would be there
at anytime.
Yes, that's quite true. However are we absolutely sure that this class must only be created once in every single context that it appears? What about test code? It is often very difficult to write tests for code that uses singletons because of their global nature.
I have seen quite a few systems start with singletons only to realize that there was a desirable generalization in which there could be more than one of the objects. So you have risks in both directions. If you create a singleton you risk needing more than one. If you don't create singletons you risk creating more than one.
So, for my money, I prefer communication within the group as opposed to enforcing singletons.
Many people say that Patterns are complex and should not be used in
small system (close-knit).They are only useful in big Framework made up
of using xyz people team and $xyz million amount etc etc...
I disagree. Patterns are patterns. You may find a certain pattern useful or not in any particular context. There is no context in which patterns, in general, do not apply.
--
Robert C. Martin (Uncle Bob) | email: unclebob@xxxxxxxxxxxxxxxx
Object Mentor Inc. | blog: www.butunclebob.com
The Agile Transition Experts | web: www.objectmentor.com
800-338-6716 |
.
- Follow-Ups:
- Re: Singletons
- From: David Barrett-Lennard
- Re: Singletons
- From: raxitsheth2000
- Re: Singletons
- References:
- Re: Singletons
- From: Robert Martin
- Re: Singletons
- From: raxitsheth2000
- Re: Singletons
- Prev by Date: Re: The low down on Efision?
- Next by Date: Re: More buzz wanted
- Previous by thread: Re: Singletons
- Next by thread: Re: Singletons
- Index(es):
Relevant Pages
|