Re: singleton vs static

From: Robert C. Martin (unclebob_at_objectmentor.com)
Date: 05/22/04


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



Relevant Pages

  • Re: Are global variable evil?
    ... I think that there is no ideal solution, at least not one that works ... > With regards to the static class method (not encapsulating a singleton) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: enum or static class
    ... I found it hard to use enums for this purpose and feel more ... If you are talking a "property bag" type setup, having a static class, ... user, you can use session, or at least cache by session id, etc. ... If they are truly constants for the app, spinning up a singleton for the ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Singleton pattern vs Class with purely static members
    ... > static class, is when resources that are expensive need to be released as ... IMO the main difference is that with a singleton pattern the code using it ... In the case of a static class the calling code call the methods using the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Singleton types and static class
    ... all client requests are serviced by that instance. ... the server creates an instance and all subsequent ... The singleton pattern doesn't in and of itself dictate a finite lifetime, but of course if you are using some framework that defines its own kind of singleton it might impose something like that. ... A singleton offers a slightly more abstracted "lazy initialization" implementation, but even with a static class you can initialize any underlying data structures needed to support the implementation lazily, so there's not much practical difference in that respect. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Superclass of eigenclass
    ... just inconsistent semantics and bad terminology. ... Take, for example, a "Ruby in the browser" (perhaps one ... And the eigenclass would be purely a ... the term singleton class would ...
    (comp.lang.ruby)