Detailed explanation of Asp. Net Design Mode in singleton mode, asp.net Design Mode

Source: Internet
Author: User

Detailed explanation of Asp. Net Design Mode in singleton mode, asp.net Design Mode

This article shares with you the singleton mode of Asp. Net Design Patterns for your reference. The specific content is as follows:

I. design purpose:Show only one instance object in the project

Ii. design steps:

Create a class;
Construct static variables of the class type;
Defines a static method with a return value class as a singleton type;
Judge whether the static variable instance is null: If it is null, create an instance and assign values to the fields in the singleton class. If it is not null, return this variable (return instance ;)
The Singleton mode is successfully created;

Iii. project-related terms or knowledge points

Class type: if the current class is SingleCase, the static variable type must also be SingleCase, that is, the type of the static variable defined here must be the same as the type of the current class.

When defining Singleton instance variables, use instances whenever possible to refer to them, so that you and others can read the code in the future.

There is only one static variable in the memory.

Iv. Source Code

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace SingleCaseModel00 {// <summary> // 1. create such a class // </summary> class SingleCase {// 2. construct static variables of the class type // class type: if the current class is SingleCase, the type of the secondary static variable must also be SingleCase, // that is, the type of the static variable defined here must be the same as the type of the current class. // when defining the single-instance variable, use the instance whenever possible, easy to read // there is only one static variable in the memory and only occupies one part. public static SingleCase instance = null; // 10. define a field to test public string name; // 3. defines the return value class as the static method public static SingleCase getInstance (string n) {// 4. judge whether the static variable instance is null if (instance = null) {// 5. if it is null, create instance = new SingleCase (); // 6. assign an instance value to a field in the singleton class. name = n;} // 7. return this variable return instance;} // 8. if there are other fields in this class, write them directly below // as follows: // public string Name; // public int Age ;} class Program {static void Main (string [] args) {// 9. call the method in the preceding SingleCase class to test SingleCase s1 = SingleCase. getInstance ("I uploaded the parameter for the first time"); SingleCase s2 = SingleCase. getInstance ("I'm the second parameter"); Console. writeLine (s1.name); Console. writeLine (s2.name); Console. readKey ();}}}

V. Result Display

6. Explanation of the results

The results of the two outputs are the same, because when the parameter is input for the first time, the instance is empty. After the above method is executed, the value of name is changed to "I passed the parameter for the first time". When you pass the parameter again for the second time, because there is only one static variable in the memory, it only occupies one part, so now the instance is no longer empty, and the instance value of the first parameter will be output directly. Therefore, no matter how many times you enter the parameter, the output result is the instance value output for the first time, which is the mechanism of the singleton mode.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.