Singleton mode, which is included in the Creative Mode series.
Creative mode indicates how and when objects are created. The Singleton pattern guarantees that a class has and only one instance, and provides a global access point to access it. In the process of programming, there are many situations where you need to ensure that a class can have only one instance. For example, there can be only one window manager, one print spool, or one data engine access point in the system. There may be several serial ports in the PC, but there can be only one COM1 instance.
The structure is as follows:
We can define a spooler class to implement the singleton pattern
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 ' establishes and saves this instance
Glbspooler = Me ' Save instance
Spool_counter = spool_counter + 1 ' count
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 ' instance already exists
Finally
Getspooler = Glbspooler ' Returns a unique instance
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 class
Public Class Spoolerexception
Inherits Exception
Private MESG as String
'---------
Public Sub New ()
MyBase.New ()
MESG = "Only one instance can be created!" "
End Sub
'---------
Public Overrides ReadOnly Property message () as String
When you click the "Create Instance" button, call the Spooler class's Getspooler method and try to create the instance. In the spooler constructor, the spool_counter variable is defined, which is used to compute the number of spooler instances, and if 0 (that is, the spooler instance has not yet been created), a spooler instance is created and saved. Then add the value of the counter, and if you click the "Create Instance" button again, the exception is thrown because the number of spooler instances is 1, so you can control and create a unique instance.
The constructor functions are as follows:
Private Sub New ()
MyBase.New ()
If spool_counter = 0 Then ' establishes and saves this instance
Glbspooler = Me ' Save instance
Spool_counter = spool_counter + 1 ' count
Legalinstance = True
Else
Legalinstance = False
Throw New spoolerexception
End If
End Sub
You can control the creation of any number of spooler instances by modifying the Spool_counter criterion in the constructor. This can be said to be a single piece of the expansion of the model bar! :)
Private Sub New ()
MyBase.New ()
If spool_counter <= 3 Then ' Set up and save this instance
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.