vb.net下的单例模式

单例模式确保某对象只能实例化一次,因此可以确保程序中所有对象访问同一个对象,但是也存在一个弊端,如,不能解决删除单个对象的问题等,因为缺少实际的开发运用,对性能以及单例的优缺点就不会太明白,这里重点总结一下单例模式的5种拓展实现。

原文详见:http://www.cnblogs.com/psunny/archive/2010/06/18/1760133.html

1 、单例模式的简单实现:(这种方式创建是线程不安全的)

Public notinheritable Class UserDataReaderToEntityStrategy

    Shared singleInstance As UserDataReaderToEntityStrategy = nothing

    '私有化构造函数
    Private Sub New()

    End Sub
    '创建静态方法,用于取得全局唯一实例

    Public Shared ReadOnly Property GetInstance() As UserDataReaderToEntityStrategy
        Get
            If singleInstance Is nothing Then
                singleInstance = New UserDataReaderToEntityStrategy
            End If
            Return singleInstance
        End Get
    End Property

End Class

2、安全线程的实现

Public notinheritable Class UserDataReaderToEntityStrategy

    Shared singleInstance As UserDataReaderToEntityStrategy = nothing
    Shared ReadOnly padlock As New Object()

    '私有化构造函数
    Private Sub New()

    End Sub

    Public Shared ReadOnly Property GetInstance() As UserDataReaderToEntityStrategy
        Get
            '这里添加一个锁,保证了线程的安全
            SyncLock padlock
                If singleInstance Is nothing Then
                    singleInstance = New UserDataReaderToEntityStrategy
                End If
            End SyncLock
            Return singleInstance
        End Get
    End Property

End Class


3、双重锁定

Public notinheritable Class UserDataReaderToEntityStrategy

    Shared singleInstance As UserDataReaderToEntityStrategy = nothing
    Shared ReadOnly padlock As New Object()

    '私有化构造函数
    Private Sub New()
    End Sub

    Public Shared ReadOnly Property GetInstance() As UserDataReaderToEntityStrategy
        Get
            '这里添加一个锁,保证了线程的安全

            If singleInstance Is nothing Then
                SyncLock padlock
                    If singleInstance Is nothing Then
                        singleInstance = New UserDataReaderToEntityStrategy
                    End If
                End SyncLock
            End If
            Return singleInstance
        End Get
    End Property

End Class

4、静态初始化(首选方式)

Public notinheritable Class UserDataReaderToEntityStrategy

    Shared singleInstance As UserDataReaderToEntityStrategy = nothing

    Shared Sub New()

    End Sub
    '私有化构造函数
    Private Sub New()
    End Sub

    Public Shared ReadOnly Property GetInstance() As UserDataReaderToEntityStrategy
        Get
            Return singleInstance
        End Get
    End Property

End Class


5、延迟初始化(比较常用)

Public notinheritable Class UserDataReaderToEntityStrategy

    Shared singleInstance As UserDataReaderToEntityStrategy = nothing

   
    '私有化构造函数
    Private Sub New()
    End Sub

    Public Shared ReadOnly Property GetInstance() As UserDataReaderToEntityStrategy
        Get
            Return nested.singleInstance
        End Get
    End Property
    Private Class nested
        Shared Sub New()

        End Sub
        Friend Shared ReadOnly singleInstance As New UserDataReaderToEntityStrategy()
    End Class
End Class


作者: Sunny Peng
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...