Singleton mode, Mode

Source: Internet
Author: User

Singleton mode, Mode

1 // 1. Thread unsafe 2 // public sealed class Singleton 3 // {4 // private static Singleton instance; // define a private static global variable to save the unique instance of the modified class 5 // private Singleton () {}6 // public static Singleton GetInstance () 7 // {8 /// if the class is not instantiated, create and modify instance 9 // return instance ?? (Instance = new Singleton ()); 10 //} 11 //} 12 13 14 // 2. thread security 15 // public sealed class Singleton16 // {17 // private static Singleton instance = null; 18 //// create a static read-only process auxiliary object 19 // private static readonly object _ lock = new object (); 20 // Singleton () {} 21 // public static Singleton GetInstance () 22 // {23 // lock (_ lock) 24 // {25 // return instance ?? (Instance = new Singleton ()); 26 //} 27 //} 28 //} 29 30 31 32 // 3. thread security double lock 33 // public sealed class Singleton34 // {35 // private static Singleton instance; 36 // private static object _ lock = new object (); 37 // private Singleton () {}38 // public static Singleton GetInstance () 39 // {40 // if (instance = null) // when the instance is null, lock creation 41 // {42 // lock (_ lock) // ensure that only one thread can access the modified statement block 43 // {44 // instance = instance ?? (New Singleton (); 45 //} 46 //} 47 // return instance; 48 //} 49 //} 50 51 52 53 54 // 4. Thread Security static initialization 55 // public sealed class Singleton56 // {57 // private static Singleton instance = new singleton (); 58 // private Singleton () {}59 // public static Singleton GetInstance () 60 // {61 // return instance; 62 //} 63 //} 64 65 66 // 5. Delayed initialization, internal class 67 // public sealed class Singleton68 // {69 // private Singleton () {} 70 // public static Singleton GetInstance () 71 // {72 // return Nested. instance; 73 //} 74 // The initialization work is put into the static member of the Nested class to complete 75 // private class Nested76 // {77 // static Nested () {} 78 // internal static readonly Singleton instance = new Singleton (); 79 //} 80 //} 81 82 83 // 6. Delayed initialization, thread security 84 public sealed class Singleton85 {86 private static readonly Lazy <Singleton> lazy = new Lazy <Singleton> () => new Singleton ()); 87 public static Singleton GetInstance () 88 {89 return lazy. value; 90} 91 private Singleton () {}92}

 

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.