Re: Singletons



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                  |



.



Relevant Pages

  • Re: Creating framework for singleton pattern?
    ... was presented to base an article warning off of. ... > harmful (and you don't seem opposed to that notion, and if Singletons can ... > misused by allowing the creation of global variables in an OO context, ... patterns beyond the OO purists point of view. ...
    (microsoft.public.dotnet.languages.csharp)
  • Software patterns Was: LISP is dead now ! ! !
    ... >> singletons, but because all signs point to it being one more ... > The Design Patterns book has a subtext that strongly suggests that ... But some software patterns are less localized than macros. ...
    (comp.lang.lisp)