单件模式(Singleton Pattern)vb.net
2010-10-15 21:21
141 查看
单件模式(Singleton Pattern)这个模式的技术简单明了:
(1)隐藏默认类的构造函数
(2)添加一个共享的成员,以引用单独的、实例化的对象
(3)创建一个公有函数来创建或返回被引用的对象
Public Class Mysingleton
Private Shared m_Instance As Mysingleton
Private Shared m_TimeOfBirth As String
Private Sub New()
Console.WriteLine("Creating Singleton at {0}", Now.ToLongTimeString)
m_TimeOfBirth = Now.ToLongTimeString
End Sub
Public Shared Function GetInstance() As Mysingleton
If m_Instance Is Nothing Then
m_Instance = New Mysingleton()
End If
Return m_Instance
End Function
Public ReadOnly Property TimeOfbirth() As String
Get
Return m_TimeOfBirth
End Get
End Property
End Class
Sub Main()
Dim obTest1 As Mysingleton
Dim obTest2 As Mysingleton
Dim i As Integer
obTest1 = Mysingleton.GetInstance()
Console.WriteLine("GetInstance() called at {0} ; obtest1.TimeofBirth()={1}", Now.ToLongTimeString, obTest1.TimeOfbirth)
For i = 1 To 50000000
Next
obTest2 = Mysingleton.GetInstance
Console.WriteLine("GetInstance() called at {0} ; obtest2.TimeofBirth()={1}", Now.ToLongTimeString, obTest2.TimeOfbirth)
MessageBox.Show("click ok to end")
end sub
多线程
Public Class Mysingleton
Private Shared m_Instance As Mysingleton
Private Shared m_TimeOfBirth As String
'多线程安全
Private Shared m_Mutex As New System.Threading.Mutex
Private Sub New()
Console.WriteLine("Creating Singleton at {0}", Now.ToLongTimeString)
m_TimeOfBirth = Now.ToLongTimeString
End Sub
Public Shared Function GetInstance() As Mysingleton
m_Mutex.WaitOne()
If m_Instance Is Nothing Then
m_Instance = New Mysingleton()
End If
m_Mutex.ReleaseMutex()
Return m_Instance
End Function
Public ReadOnly Property TimeOfbirth() As String
Get
Return m_TimeOfBirth
End Get
End Property
End Class
(1)隐藏默认类的构造函数
(2)添加一个共享的成员,以引用单独的、实例化的对象
(3)创建一个公有函数来创建或返回被引用的对象
Public Class Mysingleton
Private Shared m_Instance As Mysingleton
Private Shared m_TimeOfBirth As String
Private Sub New()
Console.WriteLine("Creating Singleton at {0}", Now.ToLongTimeString)
m_TimeOfBirth = Now.ToLongTimeString
End Sub
Public Shared Function GetInstance() As Mysingleton
If m_Instance Is Nothing Then
m_Instance = New Mysingleton()
End If
Return m_Instance
End Function
Public ReadOnly Property TimeOfbirth() As String
Get
Return m_TimeOfBirth
End Get
End Property
End Class
Sub Main()
Dim obTest1 As Mysingleton
Dim obTest2 As Mysingleton
Dim i As Integer
obTest1 = Mysingleton.GetInstance()
Console.WriteLine("GetInstance() called at {0} ; obtest1.TimeofBirth()={1}", Now.ToLongTimeString, obTest1.TimeOfbirth)
For i = 1 To 50000000
Next
obTest2 = Mysingleton.GetInstance
Console.WriteLine("GetInstance() called at {0} ; obtest2.TimeofBirth()={1}", Now.ToLongTimeString, obTest2.TimeOfbirth)
MessageBox.Show("click ok to end")
end sub
多线程
Public Class Mysingleton
Private Shared m_Instance As Mysingleton
Private Shared m_TimeOfBirth As String
'多线程安全
Private Shared m_Mutex As New System.Threading.Mutex
Private Sub New()
Console.WriteLine("Creating Singleton at {0}", Now.ToLongTimeString)
m_TimeOfBirth = Now.ToLongTimeString
End Sub
Public Shared Function GetInstance() As Mysingleton
m_Mutex.WaitOne()
If m_Instance Is Nothing Then
m_Instance = New Mysingleton()
End If
m_Mutex.ReleaseMutex()
Return m_Instance
End Function
Public ReadOnly Property TimeOfbirth() As String
Get
Return m_TimeOfBirth
End Get
End Property
End Class
相关文章推荐
- 设计模式与VB .net实现 桥接模式,单件模式
- VB.NET Singleton模式 单件模式
- VB.net设计模式之单件模式(singleton)
- VB.NET实现Singleton模式
- 用回拨卡打电话来看代理模式+vb.net实现
- vb.net 只读模式预览Excel文件
- VB.NET & 职责链模式 (下机时间操作)
- 通过商场促销来看策略模式+vb.net实现
- 从我出门来看装饰模式+vb.net实现
- VB.net实现Singleton模式
- VB.NET & 策略模式(下机用户类型选择)
- NET常用设计模式——单件模式
- VB.NET listview 的虚模式
- 从我出门来看装饰模式+vb.net实现
- 设计模式方法VB.NET机房个人重构版-磨刀篇(一)
- vb.net 中通过反射实现简单工厂模式
- 艾伟_转载:.NET设计模式:单件模式(Singleton Pattern)
- .Net设计模式学习笔记(二):单件模式(Singleton Pattern)
- vb.net下的单例模式
- NET中的设计模式---单件模式