Simple output Singleton Mode

Source: Internet
Author: User

From now on, learning the design model was a long time ago. At the beginning, I had a very simple understanding of the design model, which was basically superficial and did not know how to apply it in the system. Now the Group has begun to work together to build a system. Using the design pattern in the system, we can better implement code reuse, so that our system can better achieve low coupling and high inner, at the same time, it is more convenient for later extension and maintenance. In this blog, we will review and understand the Singleton mode: Singleton mode ensures that a class has only one instance and provides a global access point to it. Structure Diagram: C # implementation code: [csharp] <span style = "font-family: Microsoft YaHei; font-size: 18px "> class Singleton {// defines a private variable to save the created instance private static Singleton instance; // private constructor. You cannot use New to create such instances private Singleton () {} // provides a static method to return a unique Singleton instance public static Singleton GetInstance () {// If the instance is null, the Singleton object has not been created. // if the instance is not null, the Singleton object has been created and the method if (instance = null) is not executed) {// create a Singleton object And cache the instance = new Singleton () ;}return instance ;}</span> [csharp] <span style = "font-family: Microsoft YaHei; font-size: 18px "> // client code static void Main (string [] args) {// create a Singleton object. You cannot use the constructor, but you can only use the getInstance method Singleton s1 = Singleton. getInstance (); Singleton s2 = Singleton. getInstance (); if (s1 = s2) // compare the two instantiated objects with the same instance {Console. writeLine ("the two objects are the same instance. ") ;}Console. Read () ;}</span> the following uses the vb.net language to implement the singleton mode. First, create a Windows application. The default form is Form1, and set its ISMdiContainer attribute to true, indicating that it is a container of the Multi-Document Interface MdI subform. Create a button, create a form ChildForm, and click it to create a subform. When the singleton mode is not used, the child form will display multiple (1), but this is not the result we want, we want to show in Figure 2, no matter how many times you click, only one subform is displayed. In this case, the singleton mode is used. Figure 1 Figure 2 below we just need to follow the code above. [Csharp] Public Class ChildForm 'defines a Private variable and stores the Private Shared instance As ChildForm 'private constructor of the created instance. New cannot be used to create such an instance (we have not written it ), all classes have constructor methods. If they are not encoded, the system generates an empty constructor. A static method is provided to return a unique Singleton instance Public Shared Function CreateForm () as ChildForm 'If the instance does not exist or is closed, it indicates that the ChildForm object has not been created. If the instance exists, it indicates that the ChildForm object has been created, this method will not be executed If instance Is Nothing OrElse instance. isDisposed = True then': Creates a ChildForm object and caches it into inst. Ance = New ChildForm instance. mdiParent = Form. activeForm End If Return instance 'returns the instantiation result End Function End Class' client code Public Class Form Private Sub btnShow_Click (ByVal sender As System. object, ByVal e As System. eventArgs) Handles btnShow. click 'show subforms ChildForm. createForm (). show () End Sub End Class Singleton mode Advantages and Disadvantages: 1. Ensure that this Class only has unique instances. 2. This class stores instances and ensures that no other instances can be created. 3. Expose the instance interface to the entire system for controlled access to the unique instance. All the design patterns are the same. They have both advantages and disadvantages. While we use them, we cannot ignore them. In a multi-threaded program, multiple threads can access them at the same time, multiple instances may be created and there are security issues.

Related Article

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.