Agile Development, Architecture, .NET and The Art of Listening  RSS 2.0

Navigation
 Sunday, October 12, 2008
//SINGLETON - C#

using NUnit.Framework;

[TestFixture()]
public class SingletonTest
{
    [Test()]
    public void ObjectsAreTheSameTest()
    {
        DeploymentInfo firstDeployment = Singleton<DeploymentInfo>.UniqueInstance;
        DeploymentInfo secondDeployment = Singleton<DeploymentInfo>.UniqueInstance;
        Assert.AreSame(firstDeployment, secondDeployment);
    }
}

//Singleton Pattern 
//Generic Version

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

    private class SingletonCreator
    {
        static SingletonCreator()
        {
        }
        // Private object instantiated with private constructor
        static internal readonly T instance = new T();
    }

    public static T UniqueInstance {
        get { return SingletonCreator.instance; }
    }
}


'SINGLETON - VB.NET

Imports NUnit.Framework

<TestFixture()> _
Public Class SingletonTest
    <Test()> _
    Public Sub ObjectsAreTheSameTest()
        Dim firstDeployment As DeploymentInfo = Singleton(Of DeploymentInfo).UniqueInstance
        Dim secondDeployment As DeploymentInfo = Singleton(Of DeploymentInfo).UniqueInstance
        Assert.AreSame(firstDeployment, secondDeployment)
    End Sub
End Class

'Singleton Pattern 
'Generic Version

Public Class Singleton(Of T As {Class, New})
    Private Sub New()
    End Sub

    Private Class SingletonCreator
        Shared Sub New()
        End Sub
        ' Private object instantiated with private constructor
        Friend Shared ReadOnly instance As New T()
    End Class

    Public Shared ReadOnly Property UniqueInstance() As T
        Get
            Return SingletonCreator.instance
        End Get
    End Property
End Class

Sunday, October 12, 2008 5:40:08 PM (Central America Standard Time, UTC-06:00)  #    Comments [0] -
.NET | Patterns & Practices | Useful stuff
Comments are closed.
Archive
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2009
Vlad Navazhylau
Sign In
Statistics
Total Posts: 174
This Year: 0
This Month: 0
This Week: 0
Comments: 1
All Content © 2009, Vlad Navazhylau