VB.NET Singleton模式 单件模式

前端之家收集整理的这篇文章主要介绍了VB.NET Singleton模式 单件模式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
'Singleton模式
Public Class Singleton
    Private Shared uniqueInstance As New Singleton

    Private Sub New()

    End Sub

    Public Shared Function getInstance() As Singleton
        If uniqueInstance Is Nothing Then
            uniqueInstance = New Singleton
        End If
        Return uniqueInstance
    End Function

End Class

Public Class Singleton
    Private Shared _Singleton As Singleton = Nothing
    Private Shared _Mutex As New system.threading.Mutex '进程同步 

    Private Sub New()
        '类构造
    End Sub

    Public Shared Function Instance() As Singleton
        If _Singleton Is Nothing Then  'double-checked locking
            _Mutex.WaitOne()
            Try
                If _Singleton Is Nothing Then
                    _Singleton = New Singleton
                End If
            Finally
                _Mutex.ReleaseMutex()
            End Try
        End If
        Return _Singleton
    End Function
End Class
原文链接:https://www.f2er.com/vb/259548.html

猜你在找的VB相关文章