<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>More Singleton</title><link>http://blogs.msdn.com/theoy/archive/2004/05/21/136610.aspx</link><description>Ok, so I lied: I'm not moving on to unions quite yet. Cyrus brought up a great point on the last post about .NET serialization, as well as the lack of thread safety in my third example. Due to the shared nature of singletons, I have to agree that thread-safety</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: More Singleton</title><link>http://blogs.msdn.com/theoy/archive/2004/05/21/136610.aspx#136615</link><pubDate>Fri, 21 May 2004 10:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:136615</guid><dc:creator>Cyrus Najmabadi</dc:creator><description>Theo: &amp;quot;The example differs from both the MSDN example and the arachsys.com article, but I'm pretty convinced it's the right thing to do&amp;quot;&lt;br&gt;&lt;br&gt;Writing a single test would probably be the best thing to do here and then you would not only convince yourself, but me as well :-)</description></item><item><title>re: More Singleton</title><link>http://blogs.msdn.com/theoy/archive/2004/05/21/136610.aspx#136662</link><pubDate>Fri, 21 May 2004 12:49:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:136662</guid><dc:creator>Andrew</dc:creator><description>Theo: thanks for the summary. It's been a rather interesting discussion. I'm looking forward to your next article :)</description></item><item><title>re: More Singleton</title><link>http://blogs.msdn.com/theoy/archive/2004/05/21/136610.aspx#137113</link><pubDate>Fri, 21 May 2004 19:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:137113</guid><dc:creator>Joe Duffy</dc:creator><description>First, a question: Why are you overly concerned about controlling the lifecycle of a single object instance when it contains absolutely no state? Wouldn't a static class fulfill the same requirement (without having to worry about synchronization and other object lifecycle related concerns)?&lt;br&gt;&lt;br&gt;Second, a comment: I'm not a big fan of Singletons. The pattern is majorly overused, most likely due to its simplicity and perhaps since it's among the first patterns detailed in the GoF book. There aren't that many cases in which a truly global object is needed, and resorting to a Singleton is often just a lazy way of solving the following problem: Well, I know there's some sort of context to which this object belongs, but it's really a pain to pass the context around and I don't know how to do it in an object oriented fashion, so poof I'll create the equivallent of a global variable.&lt;br&gt;&lt;br&gt;Not trying to cause any heartburn, but I wanted to toss my opinion into the mix.</description></item><item><title>re: More Singleton</title><link>http://blogs.msdn.com/theoy/archive/2004/05/21/136610.aspx#137251</link><pubDate>Fri, 21 May 2004 19:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:137251</guid><dc:creator>Theo Yaung</dc:creator><description>Good question/comment, Joe.&lt;br&gt;&lt;br&gt;One of the nicer payoffs of a singleton pattern with true instance uniqueness is the ability to use reference equality, instead of dispatching to a .Equals() method.  Thus, although C# allows you to override the '==' operator, by creating a singleton, you don't need to.  In addition, object.ReferenceEquals(,) still works.&lt;br&gt;&lt;br&gt;A static class doesn't always fulfill the same niche as a singleton, because a class can't always substitute an object.  Sure, you can get the object that represents the classes's type by using typeof(Foo), but that object returned doesn't declare the methods that Foo provides.&lt;br&gt;&lt;br&gt;Using singleton objects instead of classes allow you to swap out that particular object.  Let me bring up the stream example again.&lt;br&gt;&lt;br&gt;TextWriter stream = Console.Out;&lt;br&gt;&lt;br&gt;if(user wants stderr instead) {&lt;br&gt;    stream = Console.Error;&lt;br&gt;}&lt;br&gt;&lt;br&gt;stream.WriteLine(&amp;quot;write my logging here...&amp;quot;);&lt;br&gt;stream.Write(&amp;quot;use a different method...&amp;quot;);&lt;br&gt;stream.WriteLine();&lt;br&gt;&lt;br&gt;One might argue that at times, delegates to methods of a static class are a reasonable implementation too.  Certainly this is the case if you're only concerned about one method (as many a .NET designer will tell you that delegates are often a good way to replace an interface that only has one method).  However, by instantiating singleton objects instead of using a static class, you can swap out the functionality of all of the methods at the same time, without doing multiple assignments -- which may be difficult to keep consistent.&lt;br&gt;&lt;br&gt;Also, you bring up a good point about global variables -- I agree, using a singleton as a global variable is almost always a horrible idea.  Use of singletons should be more like global *constants*.  People use global constants all the time.  One can argue that the idea of &amp;quot;zero&amp;quot; is a global constant, or &amp;quot;false&amp;quot; and &amp;quot;true&amp;quot; are also global constants.  The emphasis is on constant, that's why it's imperative that singletons should contain state (shouldn't mutate).&lt;br&gt;&lt;br&gt;I don't always agree with GoF too, but I think in this case, I needed to start with singleton since it (or its ideas) tend to make its ways into many other &amp;quot;patterns&amp;quot;.&lt;br&gt;&lt;br&gt;Definitely no heartburn experienced Joe, and thanks a bunch for posing these questions so we could cover these issues.  I hope the explanation is clear, but by all means ask further questions if needed.</description></item><item><title>re: More Singleton</title><link>http://blogs.msdn.com/theoy/archive/2004/05/21/136610.aspx#142964</link><pubDate>Thu, 27 May 2004 10:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:142964</guid><dc:creator>Andrew</dc:creator><description>A few more things to ponder on this topic...&lt;br&gt;&lt;br&gt;What are the implications of the following on the initialisation of the singletin:&lt;br&gt;1. Multiple processors&lt;br&gt;2. Processors with weak memory ordering (such as Intel Itanium processors)?&lt;br&gt;&lt;br&gt;Your thoughts and comments on this article:&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://discuss.develop.com/archives/wa.exe?A2=ind0203B&amp;amp;L=DOTNET&amp;amp;P=R375"&gt;http://discuss.develop.com/archives/wa.exe?A2=ind0203B&amp;amp;L=DOTNET&amp;amp;P=R375&lt;/a&gt;&lt;br&gt;&lt;br&gt;Andrew</description></item></channel></rss>