Feedback

C# - Singleton

Veröffentlicht von am 12/1/2007
(1 Bewertungen)
Generische und threadsichere Implementierung des Singleton Entwurfsmusters
#region Using Statements
using NUnit.Framework;
#endregion

namespace Helper
{
    namespace DesignPatterns
    {
        /// <summary>
        /// Implementation of the Singleton Pattern. The singleton pattern is one of 
        /// the best-known patterns in software engineering. Essentially, a singleton 
        /// is a class which only allows a single instance of itself to be created, and 
        /// usually gives simple access to that instance. Most commonly, singletons 
        /// don't allow any parameters to be specified when creating the instance - as 
        /// otherwise a second request for an instance but with a different parameter 
        /// could be problematic!
        /// http://www.yoda.arachsys.com/csharp/singleton.html
        /// </summary>
        /// <typeparam name="T">type of the singleton</typeparam>
        public sealed class Singleton<T> where T : class, new()
        {
            static T m_Instance = null;

            /// <summary>
            /// this variable is used for thread safety
            /// </summary>
            static readonly object m_Padlock = new object();

            /// <summary>
            /// returns the reference of the singleton
            /// </summary>
            public static T Instance
            {
                get
                {
                    lock (m_Padlock)
                    {
                        if (m_Instance == null)
                        {
                            m_Instance = new T();
                        }
                        return m_Instance;
                    }
                }
            }
        }
    }

    namespace UnitTests
    {
        class Account
        {
            int number;

            public int Number
            {
                get { return number; }
                set { number = value; }
            }
        }

        [TestFixture]
        public class TestSingleton
        {
            [Test]
            public void TestSingletonClass()
            {
                DesignPatterns.Singleton<Account>.Instance.Number = 34;
                Assert.AreEqual(34, DesignPatterns.Singleton<Account>.Instance.Number);
            }
        }
    }
}

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!