单例模式实现Demo
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 8 namespace SearchScore.Factory 9 { 10 public class SingleInterface 11 { 12 /// <summary> 13 /// 创建一个内部访问的单例 14 /// </summary> 15 private static volatile ExamInterface.ExamInterface singleInterface = null; 16 17 /// <summary> 18 /// 创建一个多线程锁 19 /// </summary> 20 private static object lockObj = new object(); 21 22 /// <summary> 23 /// 私有化一个单例构造函数 24 /// </summary> 25 private SingleInterface() {} 26 27 /// <summary> 28 /// 提供外部访问的一个实例化属性 29 /// </summary> 30 public static ExamInterface.ExamInterface Interface 31 { 32 get 33 { 34 if (singleInterface == null) 35 { 36 //锁住,避免多线程重复创建 37 lock (lockObj) 38 { 39 if (singleInterface == null) 40 { 41 singleInterface = new ExamInterface.ExamInterface(); 42 } 43 } 44 } 45 return singleInterface; 46 } 47 } 48 } 49 }
本文来自博客园,作者:云辰,转载请注明原文链接:https://www.cnblogs.com/yunchen/p/15040412.html