Dot Net Design Mode-single-piece Mode

Source: Internet
Author: User
Tags dot net
1. Overview
1.1 intent
The single-piece mode ensures that an application has only one globally unique instance and provides a global access point to access it.

1.2 usage
When a class can only have one instance and can be accessed globally. This unique instance can be extended by sub-classes and can be used without changing the code.
The factory class we introduced earlier is often instantiated as a globally unique single piece. Possible single pieces include the log management object, keyword generation object, and external device interface object.

1.3 Structure
The structure of the single-piece mode is very simple, including preventing the private constructor of instances created by other objects, saving the private variables and global access interfaces of the unique instance.

1.4 results
A single piece provides a globally unique access portal, so it is easy to control possible conflicts.
A single piece is an improvement of static functions. First, it avoids global variables from polluting the system. Second, the agricultural economy can have sub-classes, virtual functions, and polymorphism. Static Methods in the class cannot be defined as virtual functions, so they do not have polymorphism.
The single-piece mode can be extended to multiple, that is, multiple controlled instances are allowed to exist.

2.. NET implementation
The implementation of the single-piece mode is relatively simple. The main points are as follows.
(1) private constructor prevents external instantiation.
(2) Save the static private variables of the unique instance.
(3) initialize and obtain the static method of the unique instance.
The VB Code is as follows:
Public Class VBSingletonClass VBSingleton
Private Shared mySingleton As VBSingleton
Private Sub New ()
End Sub

Public Shared Function GetInstance () As VBSingleton
If mySingleton Is Nothing Then
MySingleton = New VBSingleton
End If
Return mySingleton
End Funtion
End Class

3. Single-piece counters
The simplest single-piece mode application is a counter. In a Web-based application, we want a global counter to count the number of clicks. Therefore, we first define a single-piece Counter: public Class CounterClass Counter
Private intCounter As Integer = 0
Private Shared myCounter As Counter
Public Function Count () As Integer
IntCounter = intCounter + 1
Return intCounter
End Function

Public Shared Function GetInstance () As Counter
If myCounter Is Nothing Then
MyCounter = New Counter
End If
Return myCounter
End Function
End Class

Add the calling code Private Sub Page () Sub Page-Load (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles MyBase. Load to the Page to be counted.
If Not Page. IsPostBack Then Me. Labell. Text = Counter. GetInstance. Count
End Sub

The following code simulates access. Each time you Click a button, the counter is added with 1: Private Sub Buttonl () Sub Buttonl-Click (ByVal semder As System. object, ByVal e As System. eventArgs) Handles Buttonl. click
Me. Labell. Text = Counter. GetInstance. Count
End Sub

This Counter is global and saved in Counter.

4. global variables and single pieces
In the above example, can we use a global variable of the form to replace the single-piece mode? We define a static global variable, which is made at the beginning of the application startup, and can then be used anywhere in the program. In this example, a form is created and opened. If we close this form, we will find that it cannot be opened again because the instance of the form has been destroyed. The only way is to create a new form, because it cannot be known which modules will call the form, so it is necessary to determine whether the form has been destroyed in all possible places? Do you need to create a new one? The amount of system maintenance can be imagined.
It can be seen that the single-piece mode maintains its own instantiation, which is safe for use. If a global object cannot be maintained by itself, it cannot avoid repeated creation of multiple instances, and system resources will be greatly occupied. Even worse, there may be logic problems in many cases. When these objects access the same resources (such as serial ports), access conflicts may occur.

5. Static methods in a single piece and category
The metadata class provides a public static method of the system, and often uses private constructors. Unlike a single piece, it does not have an instance, and all the methods are static methods.
The difference between a category class and a single piece is as follows.
(1) The lifecycle class does not save the status and only provides functions.
(2) The category class does not have polymorphism, but a single piece can have subclasses.
(3) A single piece is an object, and the category class is only a collection of methods.
It should be said that in practical application, the category class is more widely used, and a single piece should be used when objects are involved. For example, can I replace an abstract factory with a category? If we use the traditional implementation method, it is obviously not possible. Because the struct class has no polymorphism, the interface of each specific factory is different. In this case, the factory object must be treated as a single piece.
If the reflection factory is used, the reflection factory can be used as the runtime class instead of the single-piece mode. Because you only need to know the name of the instantiated subclass to complete the instantiation, the specific factory and the abstract factory have been integrated, polymorphism is no longer necessary.
Therefore, when a single piece is used, and when the sequence class is used, the specific situation should be analyzed and cannot be generalized. Of course, if the handler class is treated as a single processing, it is feasible in implementation, but there is no need to do so.

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.