Design Patterns - Generic Singleton Pattern

Posted Tuesday, July 15, 2008 4:43 PM by CumpsD

A little follow up from yesterday's Singleton Pattern, where I asked for some help on how you would approach a generic singleton.

With the help of Andrew Stevenson and ExNihilo, we came up with the following Generic Singleton Pattern:

Generic Singleton Pattern

Read more at http://blog.cumps.be/design-patterns-generic-singleton-pattern/

Comments

# re: Design Patterns - Generic Singleton Pattern

Tuesday, July 15, 2008 11:36 AM by Nish

Uhm, that's way too much work to achieve what you want.

See sanity-free.org/.../generic_singleton_pattern_in_csharp.html for a simpler solution.

# re: Design Patterns - Generic Singleton Pattern

Tuesday, July 15, 2008 12:09 PM by Jeff Gonzalez

Why not use new() instead?  Is there some reason you had to use reflection?

Change your public class declaration to:

public class Singleton<T> where T :class, new()

then change your _instance instantiation to:

private static readonly T _instance = new T();

# re: Design Patterns - Generic Singleton Pattern

Tuesday, July 15, 2008 12:11 PM by Matteo Casati

Hi David,

what about my solution?

public abstract class Singleton<T> where T : class, new()

{

   protected Singleton() { }

   private static readonly object _sync = new object();

   private static T _instance;

   public static T Instance

   {

       get

       {

           if (_instance == null)

           {

               lock (_sync)

               {

                   if (_instance == null)

                       _instance = new T();

               }

           }

           return _instance;

       }

   }

}

// usage sample:

public class MyClass : Singleton<MyClass>

{

   protected MyClass() {}

   // ...

}

Let me know!

# re: Design Patterns - Generic Singleton Pattern

Tuesday, July 15, 2008 12:27 PM by Matteo Casati

Oups.... pls, remove my previous comment: it's sooooooooo bad!

1) double-check locking is outdated :-)

2) : where new() and protected paremeterless constructor???

Maybe i was drunk!!!

Sorry.

# re: Design Patterns - Generic Singleton Pattern

Tuesday, July 15, 2008 12:43 PM by CumpsD

I'll answer each of you :)

Nish, in your case T needs a public constructor, while a true singleton can never have that. If you read the bottom part of my previous post, you can see the problem with this: blog.cumps.be/design-patterns-singleton-pattern

Jeff, same comment, the entire problem is the fact that T shouldn't be new(), since then a sloppy developer could just instantiate T himself, instead of going through the singleton.

Matteo, you got it mate :)

# re: Design Patterns - Generic Singleton Pattern

Tuesday, July 15, 2008 1:53 PM by Jeff Gonzalez

I figured I was missing some basic component of what you were trying to accomplish.   Makes perfect sense now.

# re: Design Patterns - Generic Singleton Pattern

Tuesday, July 15, 2008 2:21 PM by Nish

Ah - good point. Sorry I missed that.

# rascunho &raquo; Blog Archive &raquo; links for 2008-07-16

Wednesday, July 16, 2008 4:37 PM by rascunho » Blog Archive » links for 2008-07-16

Pingback from  rascunho  &raquo; Blog Archive   &raquo; links for 2008-07-16