Single-piece mode of design mode (Singleton pattern)

Source: Internet
Author: User
Tags constructor spl
Design a single piece mode

Singleton pattern



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

Get

message = MESG

End Get

End Property

End Class



Using a single piece mode



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 = "Create instance!" "

Catch ex as Exception

Errorbox ("instance has been created, and only one instance can be created!") ")

End Try

End Sub



Private Sub Print_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Print.click

Try

Spl.print ("instance has been created, and you clicked the button!") ")

Catch ex as Exception

Errorbox ("No instance created, cannot execute!") ")

End Try

End Sub



Run



As shown in figure:






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

Glbspooler = Me ' Save instance

Spool_counter = spool_counter + 1 ' count

Legalinstance = True

Else

Legalinstance = False

Throw New spoolerexception

End If

End Sub









Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.