VB.NET Singleton模式 单件模式

来源:互联网 发布:网络作家纪达 编辑:程序博客网 时间:2024/05/22 07:43

'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 FunctionEnd 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 FunctionEnd Class

原创粉丝点击