Generic Singleton - any suggestions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Ducker

    #1

    Generic Singleton - any suggestions?

    I want to make a whole bunch of classes into singletons. They all
    descend from a base class.

    I'd like to have code in the base class along the lines of :
    private This(){ }
    private static readonly This instance = new This();
    public static This Instance
    {
    get
    {
    return instance;
    }
    }

    but there's no way to get the type of the current class and use that
    as the equivalent of a generic.

    Any suggestions?

    Andy

  • Marc Gravell

    #2
    Re: Generic Singleton - any suggestions?

    Jon Skeet's article on implementing a singleton can easily be
    adapted to use generics. Just make sure that the type T has a
    constraint of new so that you can create the instance.
    But if it qualifies for "new()", I'm not sure you can really say it is
    a singleton in the strictest sense - just that there is a static
    instance available.

    Marc


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Generic Singleton - any suggestions?

      Jon Skeet's article on implementing a singleton can easily be adapted to
      use generics. Just make sure that the type T has a constraint of new so
      that you can create the instance. Here is the link:




      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Andrew Ducker" <andrew@ducker. org.ukwrote in message
      news:1186491660 .629607.64130@g 4g2000hsf.googl egroups.com...
      >I want to make a whole bunch of classes into singletons. They all
      descend from a base class.
      >
      I'd like to have code in the base class along the lines of :
      private This(){ }
      private static readonly This instance = new This();
      public static This Instance
      {
      get
      {
      return instance;
      }
      }
      >
      but there's no way to get the type of the current class and use that
      as the equivalent of a generic.
      >
      Any suggestions?
      >
      Andy
      >

      Comment

      • Andrew Ducker

        #4
        Re: Generic Singleton - any suggestions?

        On Aug 7, 2:57 pm, "Marc Gravell" <marc.grav...@g mail.comwrote:
        Jon Skeet's article on implementing a singleton can easily be
        adapted to use generics. Just make sure that the type T has a
        constraint of new so that you can create the instance.
        >
        But if it qualifies for "new()", I'm not sure you can really say it is
        a singleton in the strictest sense - just that there is a static
        instance available.
        Yeah, that was my problem. I could do all of it bar enforcing it
        being an actual singleton.

        Andy

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Generic Singleton - any suggestions?

          Andrew Ducker <andrew@ducker. org.ukwrote:
          But if it qualifies for "new()", I'm not sure you can really say it is
          a singleton in the strictest sense - just that there is a static
          instance available.
          >
          Yeah, that was my problem. I could do all of it bar enforcing it
          being an actual singleton.
          I don't believe there's any way of achieving what you want particularly
          easily.

          However, you can make the singleton pattern *really* short by using a
          field rather than a property:

          public class Singleton
          {
          public static readonly Singleton Instance = new Singleton();

          Singleton(){}
          }

          As it's read-only, it's not violating encapsulation very much. You
          can't use it for databinding, or put a breakpoint on it, but many of
          the other disadvantages of exposing fields don't really apply. (As an
          example of this being done in the framework, String.Empty is a field.)

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          Working...