詳細解讀VB.NET實現(xiàn)Singleton模式操作步驟
作者:佚名
VB.NET實現(xiàn)Singleton模式的代碼編寫方式會在文章中做一個詳細的介紹。大家可以通過對代碼的解讀,輕松掌握其中的應(yīng)用技巧。
我們會在這篇文章中為大家詳細介紹VB.NET實現(xiàn)Singleton模式的具體方式,希望能對大家有所幫助。首先就先從一段代碼示例來對此進行星系的分析。Singleton模式:一個類只會創(chuàng)建一個實例。#t#
下面的代碼就是VB.NET實現(xiàn)Singleton模式:
- Public Class SingletonClass Singleton
- Private Shared _Singleton As Singleton
- Private Shared _Mutex As New System.
Threading.Mutex '同步基元也可用于進程間同步 - Private Sub New()Sub New()
- End Sub
- Public Shared Function GetInstance()
Function GetInstance() As Singleton - _Mutex.WaitOne() '當在派生類中重寫時,
阻塞當前線程,直到當前的 System.Threading.
WaitHandle 收到信號 - Try
- If _Singleton Is Nothing Then
- _Singleton = New Singleton
- End If
- Finally
- _Mutex.ReleaseMutex() '釋放 System.
Threading.Mutex 一次 - End Try
- Return _Singleton
- End Function
- End Class
VB.NET實現(xiàn)Singleton模式的相關(guān)實現(xiàn)方式就為大家介紹到這里。
責任編輯:曹凱
來源:
博客園