单件模式
Singleton Pattern
Singleton 模式,它包含在创造性模式系列中。
创造性模式指示如何以及何时创建对象。Singleton 模式可以保证一个类有且只有一个实例,并提供一个访问它的全局访问点。在程序设计过程中,有很多情况需要确保一个类只能有一个实例。例如,系统中只能有一个窗口管理器、一个打印假脱机,或者一个数据引擎的访问点。PC机中可能有几个串口,但只能有一个COM1实例。
其结构如下:
我们可以定义一个Spooler类,实现Singleton 模式
Public Class Spooler
Private Shared Spool_counter As Integer
Private Shared glbSpooler As Spooler
Private legalInstance As Boolean
'-----
Private Sub New()
MyBase.New()
If Spool_counter = 0 Then '建立并且保存这个实例
glbSpooler = Me '保存实例
Spool_counter = Spool_counter + 1 '计数
legalInstance = True
Else
legalInstance = False
Throw New SpoolerException
End If
End Sub
'-----
Public Shared Function getSpooler() As Spooler
Try
glbSpooler = New Spooler()
Catch e As Exception
Throw e '实例已经存在
Finally
getSpooler = glbSpooler '返回唯一的实例
End Try
End Function
'-----
Public Sub Print(ByVal str As String)
If legalInstance Then
MessageBox.Show(str)
Else
Throw New SpoolerException()
End If
End Sub
'-----
End Class
SpoolerException类
Public Class SpoolerException
Inherits Exception
Private mesg As String
'---------
Public Sub New()
MyBase.New()
mesg = "只能创建一个实例!"
End Sub
'---------
Public Overrides ReadOnly Property Message() As String
Get
Message = mesg
End Get
End Property
End Class
使用单件模式
Private Spl As Spooler
Private Sub ErrorBox(ByVal mesg As String)
MessageBox.Show(mesg, "Spooler Error", MessageBoxButtons.OK)
End Sub
Private Sub btGetSpooler_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btGetSpooler.Click
Try
Spl = Spooler.getSpooler
TextBox1.Text = "创建实例!"
Catch ex As Exception
ErrorBox("实例已经创建,并且只能创建一个实例!")
End Try
End Sub
Private Sub Print_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Print.Click
Try
Spl.Print("实例已经创建,并且你单击了按钮!")
Catch ex As Exception
ErrorBox(&
Singleton Pattern
Singleton 模式,它包含在创造性模式系列中。
创造性模式指示如何以及何时创建对象。Singleton 模式可以保证一个类有且只有一个实例,并提供一个访问它的全局访问点。在程序设计过程中,有很多情况需要确保一个类只能有一个实例。例如,系统中只能有一个窗口管理器、一个打印假脱机,或者一个数据引擎的访问点。PC机中可能有几个串口,但只能有一个COM1实例。
其结构如下:
我们可以定义一个Spooler类,实现Singleton 模式
Public Class Spooler
Private Shared Spool_counter As Integer
Private Shared glbSpooler As Spooler
Private legalInstance As Boolean
'-----
Private Sub New()
MyBase.New()
If Spool_counter = 0 Then '建立并且保存这个实例
glbSpooler = Me '保存实例
Spool_counter = Spool_counter + 1 '计数
legalInstance = True
Else
legalInstance = False
Throw New SpoolerException
End If
End Sub
'-----
Public Shared Function getSpooler() As Spooler
Try
glbSpooler = New Spooler()
Catch e As Exception
Throw e '实例已经存在
Finally
getSpooler = glbSpooler '返回唯一的实例
End Try
End Function
'-----
Public Sub Print(ByVal str As String)
If legalInstance Then
MessageBox.Show(str)
Else
Throw New SpoolerException()
End If
End Sub
'-----
End Class
SpoolerException类
Public Class SpoolerException
Inherits Exception
Private mesg As String
'---------
Public Sub New()
MyBase.New()
mesg = "只能创建一个实例!"
End Sub
'---------
Public Overrides ReadOnly Property Message() As String
Get
Message = mesg
End Get
End Property
End Class
使用单件模式
Private Spl As Spooler
Private Sub ErrorBox(ByVal mesg As String)
MessageBox.Show(mesg, "Spooler Error", MessageBoxButtons.OK)
End Sub
Private Sub btGetSpooler_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btGetSpooler.Click
Try
Spl = Spooler.getSpooler
TextBox1.Text = "创建实例!"
Catch ex As Exception
ErrorBox("实例已经创建,并且只能创建一个实例!")
End Try
End Sub
Private Sub Print_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Print.Click
Try
Spl.Print("实例已经创建,并且你单击了按钮!")
Catch ex As Exception
ErrorBox(&
| 对此文章发表了评论 |

