Single-instance mode for ASP. NET design mode

Source: Internet
Author: User

First, design purpose: To show only one instance object in the project

Second, the design steps:

    1. Create a class;
    2. Construct static variables of class type;
    3. A static method that defines the return value class as a singleton type;
    4. Determine if the static variable instance is empty: if it is empty, create an instance and assign a value to the field in the Singleton class, or null: returns this variable (return instance;).
    5. The singleton mode is created successfully;

Third, the project related terms or knowledge point interpretation

    • The class type here: If the current class is singlecase, this static variable type is also singlecase type.
    • That is, the type of the static variable defined here is the same as the type of the current class
    • When defining a singleton instance variable, try to use instance (instance) to refer to it, so that you and others can read the code later.
    • Static variables only have one in memory, only one portion

Iv. related source code

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceSingleCaseModel008 {9     /// <summary>Ten     ///1. Create a class like this One     /// </summary> A     classSinglecase -     { -         //2. Building a class-type static variable the         //The class type here: If the current class is singlecase, then the secondary static variable type is also the singlecase type, -         //that is, the type of the static variable defined here is the same as the type of the current class -         //when defining a singleton instance variable, use instance (instance) as much as possible to refer to it for easy reading -         //static variables only have one in memory, only one portion +          Public StaticSinglecase instance =NULL; -         //10. Define a field to be used for testing +          Public stringname; A         //3. Define a static method that returns a value class as a singleton type at          Public StaticSinglecase getinstance (stringN) -         { -             //4. Determine if the static variable instance is empty -             if(Instance = =NULL) -             { -                 //5. If it is empty, create an instance inInstance =Newsinglecase (); -                 //6. Assigning values to fields in a singleton class toInstance.name =N; +             } -             //7. Return this variable the             returninstance; *         } $         //8. If there are other fields in this class, write them directly below.Panax Notoginseng         //as follows: -         //public string Name; the         //public int age; +     } A     class Program the     { +         Static voidMain (string[] args) -         { $             //9. Call the methods in the above singleton class to test the Singleton class $Singlecase S1 = singlecase.getinstance ("This is the first time I've passed the parameters."); -Singlecase s2 = singlecase.getinstance ("This is the second time I've passed the parameters."); - Console.WriteLine (s1.name); the Console.WriteLine (s2.name); - Console.readkey ();Wuyi         } the     } -}

V. Display of results

Vi. Explanation of the results

The result of the two output is the same, because when the first pass in the parameter, instance is empty, after executing the above method, the value of name becomes "I am the first pass over the parameter",

The second time you pass in the parameter, because the static variable in memory only one, only one portion, so at this time instance is not empty, but also directly output the first parameter is the instance value,

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

Single-instance mode for ASP. NET design mode

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.