Re: singleton vs static
From: Robert C. Martin (unclebob_at_objectmentor.com)
Date: 05/22/04
- Next message: SP: "Re: Object interaction and calculation"
- Previous message: Robert C. Martin: "Re: Object interaction and calculation"
- In reply to: Karl Hungus: "singleton vs static"
- Next in thread: Shane Mingins: "Re: singleton vs static"
- Reply: Shane Mingins: "Re: singleton vs static"
- Reply: Daniel Parker: "Re: singleton vs static"
- Reply: iamfractal_at_hotmail.com: "Re: singleton vs static"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 22 May 2004 15:18:53 -0500
On Sat, 22 May 2004 17:57:30 GMT, "Karl Hungus"
<nnnnndddddd@hotmail.com> wrote:
>
>What would you say is the real benefit of a singleton over a static class?
>Many times I find myself considering a singleton and then realizing a static
>class would work just as well.
I spend a lot of time talking about this while discussing the
Singleton pattern in my book "Agile Software Development: Principles,
Patterns, and Practices" (www.objectmentor.com/PPP) The chapter on
Singleton is available at
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.
* Obvious semantics. Everybody knows the class is a Singleton.
Disadvantages of Singleton
* There is no universal way to destroy the singleton.
* Derivatives of Singletons are not necessarily Singletons
* Dangling references are possible.
* Intrusive Semantics, changing from singleton to
non-singleton could break a lot of code.
Advantages of static class (Monostate)
* Destruction is not a problem.
* Hidden semantics, nobody has to know that the class is
static. Methods can be non-static even though variables
are static.
* Methods can be polymorphic.
* Derivatives of static classes are static classes.
Disadvantages of static class.
* You cannot derive a static class from a non-static class.
* You pay for creation even if you don't use it.
* Users may not realize they are using a static class.
BTW, there is another option. If you are working in a reasonably
small team, and you trust your team members, then get them all
together and explain to them that you intend to create only one of
this particular kind of object. If everybody agrees to obey that
convention, then you don't have to do anything special at all.
-----
Robert C. Martin (Uncle Bob)
Object Mentor Inc.
unclebob @ objectmentor . com
800-338-6716
"The aim of science is not to open the door to infinite wisdom,
but to set a limit to infinite error."
-- Bertolt Brecht, Life of Galileo
- Next message: SP: "Re: Object interaction and calculation"
- Previous message: Robert C. Martin: "Re: Object interaction and calculation"
- In reply to: Karl Hungus: "singleton vs static"
- Next in thread: Shane Mingins: "Re: singleton vs static"
- Reply: Shane Mingins: "Re: singleton vs static"
- Reply: Daniel Parker: "Re: singleton vs static"
- Reply: iamfractal_at_hotmail.com: "Re: singleton vs static"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|