您的位置:首页 > 编程语言 > VB

VB.NET Singleton模式 单件模式

2012-09-14 12:08 337 查看
'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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息