Re: singleton vs static
From: Daniel Parker (danielaparker_at_spam?nothanks.windupbird.com)
Date: 05/24/04
- Next message: Daniel Parker: "Re: singleton vs static"
- Previous message: Nick Landsberg: "Re: Nearest Common Ancestor Report (XDb1's $1000 Challenge)"
- In reply to: Robert C. Martin: "Re: singleton vs static"
- Next in thread: Daniel Parker: "Re: singleton vs static"
- Reply: Daniel Parker: "Re: singleton vs static"
- Reply: Robert C. Martin: "Re: singleton vs static"
- Reply: Graham Perkins: "Re: singleton vs static"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 23 May 2004 22:45:13 -0400
"Robert C. Martin" <unclebob@objectmentor.com> wrote in message
news:1vcva0ps0m9focabi1tvmdiqe6ng9r12mm@4ax.com...
>
> www.objectmentor.com/resources/articles/SingletonAndMonostate.pdf
>
> Advantages of Singleton:
>
> * Derivatives of non-singletons can be singletons
> * Lazy evaluation. If you don't use it, it doesn't get created.
Depends on the implementation. It may be advantageous in C++ and Java to
force instantiation at initialization to deal with threading issues without
locking getInstance.
> * Obvious semantics. Everybody knows the class is a Singleton.
>
> Disadvantages of Singleton
> * There is no universal way to destroy the singleton.
It usually doesn't matter. In C++, if you need the destructor of the
instance to be called when the application terminates, you can use a smart
pointer for the static instance. If the singleton contains state that needs
to be saved, there likely needs to be logic at shut down for that anyway.
> * Derivatives of Singletons are not necessarily Singletons
I have never seen anyone derive from a Singleton. Surely they should be
declared final, where the language supports that.
> * Dangling references are possible.
Are you referring to threading issues with getInstance? Or just that the
static instance has outlived its usefulness?
> * Intrusive Semantics, changing from singleton to
> non-singleton could break a lot of code.
It can be straight forward, getInstance() replaced by getInstance(type),
say. But generally, changing from one instance to many instances is a
significant change in application design.
I'm surprized you don't discuss threading issues. It still seems to come as
a surprize to many C++ and Java programmers that the standard Singleton
implementations, e.g. those in your article, the Patterns book, and in Scott
Meyers' books, are not thread safe. There are still programmers who know
about the double locked checking pattern but do not know that it does not
work in C++ and Java. (It's interesting that it can be made to work in C#.)
>
> Advantages of static class (Monostate)
Can't comment, I've never used them and I've never seen them used outside of
your articles (first in the C++ Journal.) Keep in mind that Java developers
are going to find your use of the term "static class" confusing!
Regards,
Daniel Parker
- Next message: Daniel Parker: "Re: singleton vs static"
- Previous message: Nick Landsberg: "Re: Nearest Common Ancestor Report (XDb1's $1000 Challenge)"
- In reply to: Robert C. Martin: "Re: singleton vs static"
- Next in thread: Daniel Parker: "Re: singleton vs static"
- Reply: Daniel Parker: "Re: singleton vs static"
- Reply: Robert C. Martin: "Re: singleton vs static"
- Reply: Graham Perkins: "Re: singleton vs static"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|