C # Data Center reconstruction-singleton Mode

Source: Internet
Author: User

C # Data Center reconstruction-singleton Mode
Preface

Before Data Center reconstruction, we learned the design model. In this reconstruction, our task is to add these models to the data center reconstruction. Now we can solve the simplest problem: the supergeneration of forms.

If no restriction is imposed, the result is as follows:

Very unfriendly. So how can we solve this problem? -- Singleton mode.

Singleton Mode

Generally, we can make a global variable to make an object accessible, but it cannot prevent you from instantiating multiple objects. The best way is to let the class itself save its unique instance, this class can ensure that no other instance can be created, and it can provide a method to access the instance.

Usage

There are two understandings here. A correct one.

First

First, place the declaration in the global variables of the class, and then write a method to determine whether the instantiation has been completed. This method is called when the form is required to be displayed. The Code is as follows: (Register)

 

Private frmRegister fr; // declare the global variable private void btnRegister_Click (Object sender, EventArgs e) {openRegister ();} private void openRegister () // Judgment Method {if (ftb = null | ftb. isDisposed) {ftb = new FormToolbox (); ftb. mdiParent = this; ftb. show ();}}
Obviously, this is a process-oriented thinking. Whether a form is instantiated or not requires judgment by the main form. What we need is the object-oriented thinking. Therefore, although the singleton mode function can be implemented, it is not the singleton mode. Second

Similarly, static class variables need to be declared, but this time they are declared in sub-forms, and then a private method is constructed, so that external code cannot instantiate it through new, that is to say, only the form is eligible for instantiation, and the static method is written in the class. The Code is as follows:

 

Public partial class frmRegister: Form {private static frmRegister fr = null; // declare the static class variable private frmRegister () // construct a private method {InitializeComponent ();} public static frmRegister GetInstance () // static class method. The returned value is the instance of this class {if (fr = null | fr. isDisposed) {fr = new frmRegister (); fr. mdiParent = MDIfrmMain. activeForm;} return fr ;}}
In this way, when calling the client, we only need to write: frmRegister. getInstance (). show ();. The client does not need to do any work, but calls the method as needed. In addition, the new type cannot be used to instantiate the form, because the form has only one instantiation and is saved by the class itself. When a method is called, the Class determines whether it has been instantiated. If not, a return is instantiated. If yes, a return is directly returned. Singleton mode in IDC

Although the first method does not conform to the object-oriented method, why should I write it? This has something to do with my data center. As we all know, there is an image on the main interface of the data center charging system. This requires an api function for the conversion container to perform the corresponding conversion. As a result, the Singleton mode requires more things to be applied in practice, that is, adding references to each sub-form and replacing them in each form, I think this is quite troublesome and not a good solution. Therefore, after repeated comparisons, I chose a relatively incorrect method and handed over the right of judgment to the main form.

Summary

The application of a model is not rigid. When the model is correct, it is not stuck in the format of the model. How can we make the system easier and easier to use, active Learning and practical use are the purpose of our learning design model.

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.