<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sharp World &#187; dofactory</title>
	<atom:link href="http://sharpmix.net/blog/tag/dofactory/feed/" rel="self" type="application/rss+xml" />
	<link>http://sharpmix.net/blog</link>
	<description>It&#039;s about C# and video games !</description>
	<lastBuildDate>Fri, 18 Sep 2009 15:43:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The Singleton pattern !</title>
		<link>http://sharpmix.net/blog/2009/06/12/singleton-pattern/</link>
		<comments>http://sharpmix.net/blog/2009/06/12/singleton-pattern/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 17:35:45 +0000</pubDate>
		<dc:creator>Abdallah Fdal</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[dofactory]]></category>
		<category><![CDATA[gang of four]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://managedworld.wordpress.com/?p=48</guid>
		<description><![CDATA[UPDATE : I have created a generic Singleton.
For the first programming related post, it is going to be a simple post.
The Singleton pattern,
What is this ?
If you are familiar with programming, you probably know what this is, if you are new to programming, i&#8217;ll try to explain it to you, especially what it can do [...]]]></description>
			<content:encoded><![CDATA[<h3>UPDATE : <a href="http://sharpmix.net/blog/2009/09/08/generic-singleton/">I have created a generic Singleton.</a></h3>
<p>For the first programming related post, it is going to be a simple post.</p>
<p><strong>The Singleton pattern,</strong></p>
<p><em>What is this ?</em></p>
<p>If you are familiar with programming, you probably know what this is, if you are new to programming, i&#8217;ll try to explain it to you, especially what it can do for you.</p>
<p>The <a href="http://en.wikipedia.org/wiki/Singleton_pattern">Singleton pattern</a>, as defined in Wikipedia : <em>&#8220;In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object.&#8221;</em></p>
<p>That is, when getting more comfortable with programming, you start to shape your own coding style, by creating some kind of architectures. However, there might be some times when you are confronted to some design issues. (e.g. What you coded doesn&#8217;t do what exactly what you expected when thinking about it in your brain)</p>
<p><em>Still here ?</em></p>
<p>The Singleton Pattern allows to have a unique instance of a class throughout your application.</p>
<p><em>Why do I need this ?</em></p>
<p>Here is one example I encountered when developing my DJ-ing software. Beside the business logic of my application, I had to use a library for accessing audio hardware that is inside my computer : program my sound card to fit my needs. We are going to call this thing the audio manager.</p>
<p>From a development perspective, you need to have a unique and instance of that thing because as it is said on the title it is a manager, hence, it manages one or more thing(s). So when you want to access your audio card, you generally want exclusive access to it. There could be some undesired behaviour if two &#8220;things&#8221; wants to access an audio device. Moreover, you like to have a unique access point for working with your audio card. This is where the Singleton comes in handy, a unique instance of an object, taking care of not creating other (duplicate) instances of it, returning the first instance created. (Just like in real life, there is one instance of a manager and multiple instances of employees :-)</p>
<p><span style="text-decoration: underline;">This is a very common pattern in software development.</span></p>
<p>Talking .NET now, the implementation is as follows :</p>
<p>You declare the class constructor as <em>private</em> or <em>protected</em>, (we are going to use <em>private</em> for now) this is what blocks the process of creating a new (copy) object of it. You declare a <em>static property</em> in this class, which is nothing more than a instance of the class itself. The <em>static</em> field will be initialized inline and declared as <em>readonly</em>.</p>
<p>This way of coding offers you this :</p>
<ul>
<li>By declaring it as <em>static</em>, you don&#8217;t have to create an instance of it, you can just call <em>Singleton.Instance</em>, the instance is available without creating a new object because it is declared as <em>static</em>. Now the <em>readonly</em> keyword does not allow you to modify it <span style="text-decoration: underline;">once your program has initialized</span>.</li>
</ul>
<p>The <em>readonly </em>shall not be used without creating a new object of the class :</p>
<pre class="brush: csharp;">

using System;

namespace PatternSingletonApplication
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var instance = Singleton.Instance;
            var instance1 = Singleton.Instance;

            // Test if it is the same instance.
            if (instance == instance1)
                Console.WriteLine(&quot;'instance' and 'instance1' are the same instance.&quot;);

            Console.WriteLine(&quot;Singleton.Instance can also be accessed directly,&quot;);
            Console.WriteLine(Singleton.Instance.Test());

            Console.ReadKey();
        }
    }

    public class Singleton
    {
        /// &lt;summary&gt;
        /// Protected because we don't want to expose it except for derived classes.
        /// &lt;/summary&gt;
        protected Singleton() { }

        /// &lt;summary&gt;
        /// The unique instance of the class.
        /// &lt;/summary&gt;
        public static readonly Singleton Instance = new Singleton();

        /// &lt;summary&gt;
        /// Added methods shall not be static then !
        /// &lt;/summary&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public string Test()
        {
            return &quot;Hello from Instance.Test !&quot;;
        }
    }
}
</pre>
<p>Design Patterns beginners should consider <a href="http://www.dofactory.com/Patterns/Patterns.aspx">Design Patterns in C# and VB.NET &#8211; Gang of Four (GOF)</a>. I have it and it&#8217;s great, there are nearly 70 samples showing the implementation of (all existing ?) design patterns. For about 70€ for all this, that&#8217;s cool, no ?</p>
<p>Moreover, this products offers :</p>
<ul>
<li>Structural examples</li>
<li>Real-world examples</li>
<li>.NET optimized examples</li>
</ul>
<p>(Note that I have no affiliation with the site mentioned above)</p>
]]></content:encoded>
			<wfw:commentRss>http://sharpmix.net/blog/2009/06/12/singleton-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
