Asp. Net design mode: Singleton mode; asp.net Design Mode

Source: Internet
Author: User

Asp. Net design mode: Singleton mode; asp.net Design Mode

I. design objective: to show only one instance object in the project

Ii. design steps:

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 that 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

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace SingleCaseModel00 8 {9 // <summary> 10 // 1. create such a class 11 /// </summary> 12 class SingleCase13 {14 // 2. build class type static variable 15 // class type: if the current class is SingleCase, the type of the secondary static variable must also be SingleCase, 16 // that is, the type of the static variable defined here must be the same as the type of the current class 17 // when defining the singleton instance variable, use the instance whenever possible, for ease of reading 18 // only one static variable exists in the memory and occupies only one portion 19 public static SingleCase instance = null; 20 // 10. define a field for testing 21 public string name; 22 // 3. defines the return value class as a single-instance static Method 23 public static SingleCase getInstance (string n) 24 {25 // 4. judge whether the static variable instance is null 26 if (instance = null) 27 {28 // 5. if it is null, create instance 29 instance = new SingleCase (); 30 // 6. assign 31 instances to fields in the singleton class. name = n; 32} 33 // 7. return this variable 34 return instance; 35} 36 // 8. if there are other fields in this class, you can simply write them below: 37 // as follows: 38 // public string Name; 39 // public int Age; 40} 41 class Program42 {43 static void Main (string [] args) 44 {45 // 9. call the method in the preceding SingleCase class to test SingleCase class 46 SingleCase s1 = SingleCase. getInstance ("I uploaded the parameter for the first time"); 47 SingleCase s2 = SingleCase. getInstance ("I'm the second parameter"); 48 Console. writeLine (s1.name); 49 Console. writeLine (s2.name); 50 Console. readKey (); 51} 52} 53}

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 "the parameter I passed for the first time ",

When you pass in the parameter again for the second time, the instance is no longer empty because there is only one static variable in the memory, the instance value of the first parameter is also output directly,

Therefore, no matter how many parameters you enter, the output result will be the instance value output for the first time, which is the mechanism of the singleton mode.

 

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.