<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Reacties op: Design Patterns &#8211; Singleton Pattern</title>
	<atom:link href="http://www.cumps.be/design-patterns-singleton-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cumps.be/design-patterns-singleton-pattern/</link>
	<description>Living my life...</description>
	<lastBuildDate>Sat, 12 Dec 2009 22:49:53 +0100</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>Door: Jan De Kock</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-5348</link>
		<dc:creator>Jan De Kock</dc:creator>
		<pubDate>Mon, 23 Nov 2009 12:01:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-5348</guid>
		<description>Mmm, I tried this solution in Silverlight 3 and it always threw an exception. (Exception in &#039;xxx&#039; class constructor.)
This with just an empty class with a private parameterless constructor.

Is InvokeMember limited in Silverlight 3?

Greetings</description>
		<content:encoded><![CDATA[<p>Mmm, I tried this solution in Silverlight 3 and it always threw an exception. (Exception in &#8216;xxx&#8217; class constructor.)<br />
This with just an empty class with a private parameterless constructor.</p>
<p>Is InvokeMember limited in Silverlight 3?</p>
<p>Greetings</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: Thoughts on Singleton Design Pattern &#8211; Good Bad Ugly</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-576</link>
		<dc:creator>Thoughts on Singleton Design Pattern &#8211; Good Bad Ugly</dc:creator>
		<pubDate>Mon, 17 Aug 2009 11:40:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-576</guid>
		<description>[...] Design Patterns &#8211; Singleton Pattern &#8211; A quick look at Singleton Pattern [...]</description>
		<content:encoded><![CDATA[<p>[...] Design Patterns &#8211; Singleton Pattern &#8211; A quick look at Singleton Pattern [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: David Cumps</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-575</link>
		<dc:creator>David Cumps</dc:creator>
		<pubDate>Mon, 13 Oct 2008 17:45:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-575</guid>
		<description>Hi Tim,

There&#039;s a good explanation about this on: http://www.yoda.arachsys.com/csharp/singleton.html

Check version 4 and 5

Nr 4 explains how the static way of working works, while nr 5 is just the next step after having that knowledge and making it lazy.</description>
		<content:encoded><![CDATA[<p>Hi Tim,</p>
<p>There&#8217;s a good explanation about this on: <a href="http://www.yoda.arachsys.com/csharp/singleton.html" rel="nofollow">http://www.yoda.arachsys.com/csharp/singleton.html</a></p>
<p>Check version 4 and 5</p>
<p>Nr 4 explains how the static way of working works, while nr 5 is just the next step after having that knowledge and making it lazy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: Tim Van Meerbeeck</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-574</link>
		<dc:creator>Tim Van Meerbeeck</dc:creator>
		<pubDate>Mon, 22 Sep 2008 08:31:25 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-574</guid>
		<description>[quote]As you can see, the actual instance is held inside a private sealed nested class, which is inside the GraphicsCard class. When the Instance property is called for the first time, it will instantiate a GraphicsCard in a thread-safe way, thanks to the Nested class, and upon subsequent requests, the same instance will be returned.[/quote]

Can you explain why working with an inner class makes it threadsafe?

I think when 2 threads try to access the &quot;get&quot; of the enclosing class they both are redirected to the inner class where they could access the if loop at the same time again.

Probably I am missing some basic functionality about how inner classes work but atm I would go for the adding the synchronized on top in the method signature.</description>
		<content:encoded><![CDATA[<p>[quote]As you can see, the actual instance is held inside a private sealed nested class, which is inside the GraphicsCard class. When the Instance property is called for the first time, it will instantiate a GraphicsCard in a thread-safe way, thanks to the Nested class, and upon subsequent requests, the same instance will be returned.[/quote]</p>
<p>Can you explain why working with an inner class makes it threadsafe?</p>
<p>I think when 2 threads try to access the &#8220;get&#8221; of the enclosing class they both are redirected to the inner class where they could access the if loop at the same time again.</p>
<p>Probably I am missing some basic functionality about how inner classes work but atm I would go for the adding the synchronized on top in the method signature.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: David Cumps &#187; Design Patterns - Proxy Pattern</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-573</link>
		<dc:creator>David Cumps &#187; Design Patterns - Proxy Pattern</dc:creator>
		<pubDate>Sat, 16 Aug 2008 19:09:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-573</guid>
		<description>[...] Design Patterns - Singleton Pattern [...]</description>
		<content:encoded><![CDATA[<p>[...] Design Patterns &#8211; Singleton Pattern [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: Design Patterns - Adapter Pattern - David Cumps</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-572</link>
		<dc:creator>Design Patterns - Adapter Pattern - David Cumps</dc:creator>
		<pubDate>Wed, 16 Jul 2008 22:33:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-572</guid>
		<description>[...] Design Patterns - Singleton Pattern [...]</description>
		<content:encoded><![CDATA[<p>[...] Design Patterns &#8211; Singleton Pattern [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: David Cumps &#187; Design Patterns - Adapter Pattern</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-571</link>
		<dc:creator>David Cumps &#187; Design Patterns - Adapter Pattern</dc:creator>
		<pubDate>Wed, 16 Jul 2008 22:24:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-571</guid>
		<description>[...] Design Patterns - Singleton Pattern [...]</description>
		<content:encoded><![CDATA[<p>[...] Design Patterns &#8211; Singleton Pattern [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: David Cumps</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-570</link>
		<dc:creator>David Cumps</dc:creator>
		<pubDate>Wed, 16 Jul 2008 08:54:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-570</guid>
		<description>It exists, that&#039;s about the only thing I&#039;d use it for ;) I kinda went through the same story as the one you linked.

Long ago, when I was young :p, I used the Singleton in a project, and after that I didn&#039;t touch it anymore for years, until this article.

As with most of good programming, you tend to &quot;feel&quot; things. And Singleton usually never shows up in my feelings when architecting something.</description>
		<content:encoded><![CDATA[<p>It exists, that&#8217;s about the only thing I&#8217;d use it for <img src='http://www.cumps.be/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  I kinda went through the same story as the one you linked.</p>
<p>Long ago, when I was young :p, I used the Singleton in a project, and after that I didn&#8217;t touch it anymore for years, until this article.</p>
<p>As with most of good programming, you tend to &#8220;feel&#8221; things. And Singleton usually never shows up in my feelings when architecting something.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: whaley</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-569</link>
		<dc:creator>whaley</dc:creator>
		<pubDate>Wed, 16 Jul 2008 04:15:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-569</guid>
		<description>I&#039;d encourage you to read this if you think singleton is actually useful in OO: http://steve.yegge.googlepages.com/singleton-considered-stupid</description>
		<content:encoded><![CDATA[<p>I&#8217;d encourage you to read this if you think singleton is actually useful in OO: <a href="http://steve.yegge.googlepages.com/singleton-considered-stupid" rel="nofollow">http://steve.yegge.googlepages.com/singleton-considered-stupid</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: AndrewSeven</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-560</link>
		<dc:creator>AndrewSeven</dc:creator>
		<pubDate>Tue, 15 Jul 2008 22:31:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-560</guid>
		<description>BindingFlags.Public

Nice catch :)</description>
		<content:encoded><![CDATA[<p>BindingFlags.Public</p>
<p>Nice catch <img src='http://www.cumps.be/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: David Cumps</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-568</link>
		<dc:creator>David Cumps</dc:creator>
		<pubDate>Tue, 15 Jul 2008 16:51:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-568</guid>
		<description>Ah, by the way, I also removed BindingFlags.Public, since the constructor should always be private ;)

If you by mistake are sloppy and don&#039;t add a private one (default public) or add a public one, the code will crash at runtime, giving you a chance to fix it :)</description>
		<content:encoded><![CDATA[<p>Ah, by the way, I also removed BindingFlags.Public, since the constructor should always be private <img src='http://www.cumps.be/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>If you by mistake are sloppy and don&#8217;t add a private one (default public) or add a public one, the code will crash at runtime, giving you a chance to fix it <img src='http://www.cumps.be/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: David Cumps</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-567</link>
		<dc:creator>David Cumps</dc:creator>
		<pubDate>Tue, 15 Jul 2008 16:47:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-567</guid>
		<description>Too much spam if I don&#039;t turn on comment moderation, sorry ;) And I need to eat/go out as well, added with the different timezones (most readers are from the US apparently) it can take a while to show up :p</description>
		<content:encoded><![CDATA[<p>Too much spam if I don&#8217;t turn on comment moderation, sorry <img src='http://www.cumps.be/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  And I need to eat/go out as well, added with the different timezones (most readers are from the US apparently) it can take a while to show up :p</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: AndrewSeven</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-556</link>
		<dc:creator>AndrewSeven</dc:creator>
		<pubDate>Tue, 15 Jul 2008 15:50:15 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-556</guid>
		<description>Lol, the comment dealy has a funny effect.

I work with Maxim at Orckestra

I definitely based it on the yoda.arachsys page, after I completed it, I found a few similar implementations.</description>
		<content:encoded><![CDATA[<p>Lol, the comment dealy has a funny effect.</p>
<p>I work with Maxim at Orckestra</p>
<p>I definitely based it on the yoda.arachsys page, after I completed it, I found a few similar implementations.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: Design Patterns - Generic Singleton Pattern - David Cumps</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-553</link>
		<dc:creator>Design Patterns - Generic Singleton Pattern - David Cumps</dc:creator>
		<pubDate>Tue, 15 Jul 2008 14:45:01 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-553</guid>
		<description>[...] little follow up from yesterday&#039;s Singleton Pattern, where I asked for some help on how you would approach a generic [...]</description>
		<content:encoded><![CDATA[<p>[...] little follow up from yesterday&#8217;s Singleton Pattern, where I asked for some help on how you would approach a generic [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: David Cumps</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-555</link>
		<dc:creator>David Cumps</dc:creator>
		<pubDate>Tue, 15 Jul 2008 14:15:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-555</guid>
		<description>Hi Maxim,

Thanks for your input as well. Seems Reflection is indeed the way to go :)

I updated the code and made a follow-up blog post on this Generic Singleton Pattern (dubbed like that by me right now :p)

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

Thanks for your input guys!</description>
		<content:encoded><![CDATA[<p>Hi Maxim,</p>
<p>Thanks for your input as well. Seems Reflection is indeed the way to go <img src='http://www.cumps.be/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I updated the code and made a follow-up blog post on this Generic Singleton Pattern (dubbed like that by me right now :p)</p>
<p><a href="http://blog.cumps.be/design-patterns-generic-singleton-pattern/" rel="nofollow">http://blog.cumps.be/design-patterns-generic-singleton-pattern/</a></p>
<p>Thanks for your input guys!</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: David Cumps &#187; Design Patterns - Generic Singleton Pattern</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-554</link>
		<dc:creator>David Cumps &#187; Design Patterns - Generic Singleton Pattern</dc:creator>
		<pubDate>Tue, 15 Jul 2008 14:13:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-554</guid>
		<description>[...] little follow up from yesterday&#8217;s Singleton Pattern, where I asked for some help on how you would approach a generic [...]</description>
		<content:encoded><![CDATA[<p>[...] little follow up from yesterday&#8217;s Singleton Pattern, where I asked for some help on how you would approach a generic [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: Maxim Rouiller</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-557</link>
		<dc:creator>Maxim Rouiller</dc:creator>
		<pubDate>Tue, 15 Jul 2008 12:11:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-557</guid>
		<description>From one of my coworker:

public static class TypeHelper
{
    public static T InvokeParameterlessConstructor() where T : class
    {
        return typeof(T).InvokeMember(typeof(T).Name, BindingFlags.CreateInstance &#124; BindingFlags.Instance &#124; BindingFlags.NonPublic &#124; BindingFlags.Public, null, null, null) as T;
    }
}

This is used to invoke the constructor even if the constructor is private.

However, we must make sure that the class will stay valid with this invoke so I would use it for local components but not with 3rd party.

Cheers</description>
		<content:encoded><![CDATA[<p>From one of my coworker:</p>
<p>public static class TypeHelper<br />
{<br />
    public static T InvokeParameterlessConstructor() where T : class<br />
    {<br />
        return typeof(T).InvokeMember(typeof(T).Name, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, null, null) as T;<br />
    }<br />
}</p>
<p>This is used to invoke the constructor even if the constructor is private.</p>
<p>However, we must make sure that the class will stay valid with this invoke so I would use it for local components but not with 3rd party.</p>
<p>Cheers</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: David Cumps</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-558</link>
		<dc:creator>David Cumps</dc:creator>
		<pubDate>Tue, 15 Jul 2008 07:55:14 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-558</guid>
		<description>Hi J,

Can you post a piece of your Singleton code to illustrate this please?</description>
		<content:encoded><![CDATA[<p>Hi J,</p>
<p>Can you post a piece of your Singleton code to illustrate this please?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: J</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-552</link>
		<dc:creator>J</dc:creator>
		<pubDate>Tue, 15 Jul 2008 07:01:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-552</guid>
		<description>For some unknown reason I was under the impression that using static get:ers would automatically make your code thread-safe. This would be taken care of by the CLR.

However, after reading this post I see that this is not the case :)

Anyone know where I got that from? Or if it&#039;s a misunderstanding of some sort?</description>
		<content:encoded><![CDATA[<p>For some unknown reason I was under the impression that using static get:ers would automatically make your code thread-safe. This would be taken care of by the CLR.</p>
<p>However, after reading this post I see that this is not the case <img src='http://www.cumps.be/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Anyone know where I got that from? Or if it&#8217;s a misunderstanding of some sort?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: David Cumps</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-562</link>
		<dc:creator>David Cumps</dc:creator>
		<pubDate>Mon, 14 Jul 2008 21:27:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-562</guid>
		<description>Hi ExNihilo,

Seems you had the same idea as Andrew, just started approving comments in the moderation queue ;)

Nice implementation as well. I&#039;ll be settling for the following right now, and will create another post on it to start a discussion about possible drawbacks of it.

(Code will look crappy in comments, sorry, tommorow&#039;s blog post will make it look better :p)

    public class Singleton&lt; T &gt; where T : class
    {
        protected Singleton() { }

        public static T Instance
        {
            get { return Nested.Singleton; }
        }

        private sealed class Nested
        {
            private static readonly T _instance = typeof(T).InvokeMember(typeof(T).Name,
                                                                         BindingFlags.CreateInstance &#124; BindingFlags.Instance &#124; BindingFlags.NonPublic,
                                                                         null,
                                                                         null,
                                                                         null) as T;

            internal static T Singleton
            {
                get { return _instance; }
            }
        }
    }</description>
		<content:encoded><![CDATA[<p>Hi ExNihilo,</p>
<p>Seems you had the same idea as Andrew, just started approving comments in the moderation queue <img src='http://www.cumps.be/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Nice implementation as well. I&#8217;ll be settling for the following right now, and will create another post on it to start a discussion about possible drawbacks of it.</p>
<p>(Code will look crappy in comments, sorry, tommorow&#8217;s blog post will make it look better :p)</p>
<p>    public class Singleton< T > where T : class<br />
    {<br />
        protected Singleton() { }</p>
<p>        public static T Instance<br />
        {<br />
            get { return Nested.Singleton; }<br />
        }</p>
<p>        private sealed class Nested<br />
        {<br />
            private static readonly T _instance = typeof(T).InvokeMember(typeof(T).Name,<br />
                                                                         BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic,<br />
                                                                         null,<br />
                                                                         null,<br />
                                                                         null) as T;</p>
<p>            internal static T Singleton<br />
            {<br />
                get { return _instance; }<br />
            }<br />
        }<br />
    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: David Cumps</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-566</link>
		<dc:creator>David Cumps</dc:creator>
		<pubDate>Mon, 14 Jul 2008 20:58:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-566</guid>
		<description>Hi Andrew,

Thanks for the comment! Great idea there :)

It seems you have implemented it as the following version: &#039;Fourth version - not quite as lazy, but thread-safe without using locks&#039;, from the article I linked (http://www.yoda.arachsys.com/csharp/singleton.html)

I took a moment to try and apply it to this version as well: &#039;Fifth version - fully lazy instantiation&#039;, and am happy with the result, thanks for inspiration!

It&#039;ll be included in the future solutions for this series, and I&#039;ll post an update tomorrow with credits. (Too tired now :p)</description>
		<content:encoded><![CDATA[<p>Hi Andrew,</p>
<p>Thanks for the comment! Great idea there <img src='http://www.cumps.be/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>It seems you have implemented it as the following version: &#8216;Fourth version &#8211; not quite as lazy, but thread-safe without using locks&#8217;, from the article I linked (<a href="http://www.yoda.arachsys.com/csharp/singleton.html" rel="nofollow">http://www.yoda.arachsys.com/csharp/singleton.html</a>)</p>
<p>I took a moment to try and apply it to this version as well: &#8216;Fifth version &#8211; fully lazy instantiation&#8217;, and am happy with the result, thanks for inspiration!</p>
<p>It&#8217;ll be included in the future solutions for this series, and I&#8217;ll post an update tomorrow with credits. (Too tired now :p)</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: ExNihilo</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-565</link>
		<dc:creator>ExNihilo</dc:creator>
		<pubDate>Mon, 14 Jul 2008 20:42:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-565</guid>
		<description>One way to make the generics work is to use reflection to instantiate the class. That way you don&#039;t need the public constructor, but do pay the reflection price. But given that Singletons are..instantiated only once, I think its not such a big problem.
Here&#039;s a nice example:
http://www.wijix.com/post/2008/04/Generic-C-Singleton-goodness.aspx

Though the Instance property needs to be changed, as it uses the C# 3.0 syntax. Other than that, its pretty awesome.</description>
		<content:encoded><![CDATA[<p>One way to make the generics work is to use reflection to instantiate the class. That way you don&#8217;t need the public constructor, but do pay the reflection price. But given that Singletons are..instantiated only once, I think its not such a big problem.<br />
Here&#8217;s a nice example:<br />
<a href="http://www.wijix.com/post/2008/04/Generic-C-Singleton-goodness.aspx" rel="nofollow">http://www.wijix.com/post/2008/04/Generic-C-Singleton-goodness.aspx</a></p>
<p>Though the Instance property needs to be changed, as it uses the C# 3.0 syntax. Other than that, its pretty awesome.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: AndrewSeven</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-561</link>
		<dc:creator>AndrewSeven</dc:creator>
		<pubDate>Mon, 14 Jul 2008 17:28:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-561</guid>
		<description>We use a generic helper class that is based on the nested approach, it uses reflection to access the private ctor.

public class Singleton where T : class
	{
		// Explicit static constructor to tell C# compiler
		// not to mark type as beforefieldinit
		static Singleton()
		{
		}
		public static readonly T Instance = TypeHelper.InvokeParameterlessConstructor();
	}
public static class TypeHelper
	{
		public static T InvokeParameterlessConstructor() where T : class
		{
			return typeof(T).InvokeMember
			(
			typeof(T).Name,
			BindingFlags.CreateInstance &#124; BindingFlags.Instance &#124; BindingFlags.NonPublic &#124; BindingFlags.Public,
			null, null, null
			)
			as T;
		}
	}</description>
		<content:encoded><![CDATA[<p>We use a generic helper class that is based on the nested approach, it uses reflection to access the private ctor.</p>
<p>public class Singleton where T : class<br />
	{<br />
		// Explicit static constructor to tell C# compiler<br />
		// not to mark type as beforefieldinit<br />
		static Singleton()<br />
		{<br />
		}<br />
		public static readonly T Instance = TypeHelper.InvokeParameterlessConstructor();<br />
	}<br />
public static class TypeHelper<br />
	{<br />
		public static T InvokeParameterlessConstructor() where T : class<br />
		{<br />
			return typeof(T).InvokeMember<br />
			(<br />
			typeof(T).Name,<br />
			BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,<br />
			null, null, null<br />
			)<br />
			as T;<br />
		}<br />
	}</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: David Cumps</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-563</link>
		<dc:creator>David Cumps</dc:creator>
		<pubDate>Mon, 14 Jul 2008 16:46:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-563</guid>
		<description>Hi Maxim, yeah, exactly the same thoughts here. I rarely use a singleton as well, and when I do, it&#039;s with Nested approach as well :)</description>
		<content:encoded><![CDATA[<p>Hi Maxim, yeah, exactly the same thoughts here. I rarely use a singleton as well, and when I do, it&#8217;s with Nested approach as well <img src='http://www.cumps.be/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: Maxim Rouiller</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-564</link>
		<dc:creator>Maxim Rouiller</dc:creator>
		<pubDate>Mon, 14 Jul 2008 16:31:51 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-564</guid>
		<description>Hi David,

I normally use the first approach. First reason is that I haven&#039;t had time to find a good fix for the generic version and having a public parameterless constructor is not a Singleton.

Next, I rarely need to use a Singleton myself so I often cut to the chase and just use the nested class approach without generics.

The only place I see the singleton used a lot is for data repository (see Martin Fowler Enterprise Patterns for this one) but since they are generated by an ORM I don&#039;t have to bother with that.

So for me, Solution #1.

Cheers</description>
		<content:encoded><![CDATA[<p>Hi David,</p>
<p>I normally use the first approach. First reason is that I haven&#8217;t had time to find a good fix for the generic version and having a public parameterless constructor is not a Singleton.</p>
<p>Next, I rarely need to use a Singleton myself so I often cut to the chase and just use the nested class approach without generics.</p>
<p>The only place I see the singleton used a lot is for data repository (see Martin Fowler Enterprise Patterns for this one) but since they are generated by an ORM I don&#8217;t have to bother with that.</p>
<p>So for me, Solution #1.</p>
<p>Cheers</p>
]]></content:encoded>
	</item>
	<item>
		<title>Door: Design Patterns - Singleton Pattern - David Cumps</title>
		<link>http://www.cumps.be/design-patterns-singleton-pattern/comment-page-1/#comment-559</link>
		<dc:creator>Design Patterns - Singleton Pattern - David Cumps</dc:creator>
		<pubDate>Mon, 14 Jul 2008 16:04:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cumps.be/design-patterns-singleton-pattern/#comment-559</guid>
		<description>[...] more at http://blog.cumps.be/design-patterns-singleton-pattern/  Filed under: C#, General Software Development, Visual Studio, .NET, Design [...]</description>
		<content:encoded><![CDATA[<p>[...] more at <a href="http://blog.cumps.be/design-patterns-singleton-pattern/" rel="nofollow">http://blog.cumps.be/design-patterns-singleton-pattern/</a>  Filed under: C#, General Software Development, Visual Studio, .NET, Design [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
