标签:des style blog http io color os ar 使用
1. 当系统中某个类必须仅有一个实例对象,同时访问该系统的所有访问者必须访问同一个实例对象时,且该对象实例自身占用资源又不大时。
2. 在C/s应用程序中,当我们要保证某一个非模式窗口,只能显示一个实例时。
1 /// <summary> 2 /// Description of SiteSettings. 3 /// </summary> 4 public class SiteSettings 5 { 6 private static SiteSettings instance=null; 7 private static object locker=new object(); 8 9 public SiteSettings() 10 { 11 } 12 13 public static SiteSettings Instance { 14 get { 15 if(instance==null) 16 { 17 Console.WriteLine("Install equals null."); 18 lock(locker) 19 { 20 if(instance ==null) 21 { 22 Console.WriteLine("new SiteSettings instance."); 23 instance=new SiteSettings(); 24 } 25 } 26 } 27 return instance; 28 } 29 } 30 31 private bool siteClosed=false; 32 33 public bool SiteClosed { 34 get { return siteClosed; } 35 set { siteClosed = value; } 36 } 37 }
调用:
1 class Program 2 { 3 public static void Main(string[] args) 4 { 5 Console.WriteLine("Hello World!"); 6 7 // TODO: Implement Functionality Here 8 9 Console.WriteLine(SiteSettings.Instance.SiteClosed); 10 Console.WriteLine(SiteSettings.Instance.SiteClosed); 11 SiteSettings.Instance.SiteClosed=true; 12 Console.WriteLine(SiteSettings.Instance.SiteClosed); 13 14 Console.Write("Press any key to continue . . . "); 15 Console.ReadKey(true); 16 } 17 }
运行结果:
1.) 方案1
1 /// <summary> 2 /// Description of MemberBO. 3 /// </summary> 4 public class SiteSettings 5 { 6 private static SiteSettings instance=null; 7 private static object locker=new object(); 8 9 public SiteSettings() 10 { 11 } 12 13 public static SiteSettings Instance { 14 get { 15 if(instance ==null){ 16 Console.WriteLine("Install equals null."); 17 lock(locker){ 18 Console.WriteLine("Enter locker."); 19 Console.WriteLine("new SiteSettings instance."); 20 instance=new SiteSettings(); 21 } 22 } 23 return instance; 24 } 25 } 26 27 private bool siteClosed=false; 28 29 public bool SiteClosed { 30 get { return siteClosed; } 31 set { siteClosed = value; } 32 } 33 }
2.) 方案2:
1 /// <summary> 2 /// Description of MemberBO. 3 /// </summary> 4 public class SiteSettings 5 { 6 private static SiteSettings instance=null; 7 private static object locker=new object(); 8 9 public SiteSettings() 10 { 11 } 12 13 public static SiteSettings Instance { 14 get { 15 Console.WriteLine("Install equals null."); 16 lock(locker){ 17 if(instance ==null){ 18 Console.WriteLine("Enter locker."); 19 Console.WriteLine("new SiteSettings instance."); 20 instance=new SiteSettings(); 21 } 22 } 23 return instance; 25 } 26 } 27 28 private bool siteClosed=false; 29 30 public bool SiteClosed { 31 get { return siteClosed; } 32 set { siteClosed = value; } 33 } 34 }
3.) 方案3:
1 /// <summary> 2 /// Description of MemberBO. 3 /// </summary> 4 public class SiteSettings 5 { 6 private static SiteSettings instance=null; 7 private static object locker=new object(); 8 9 public SiteSettings() 10 { 11 } 12 13 public static SiteSettings Instance { 14 get { 15 Console.WriteLine("Install equals null."); 16 if(instance ==null){ 17 Console.WriteLine("new SiteSettings instance."); 18 instance=new SiteSettings(); 19 } 20 } 21 return instance; 22 } 23 } 24 25 private bool siteClosed=false; 26 27 public bool SiteClosed { 28 get { return siteClosed; } 29 set { siteClosed = value; } 30 } 31 }
不要和Monitor,ReaderWriterLockSlim等这些线程安全的使用方式混淆。
1.) Monitor:提供同步访问对象的机制。
2.) ReaderWriterLockSlim:表示用于管理资源访问的锁定状态,可实现多线程读取或进行独占式写入访问。
1.)优点:
a.)确保了系统中保存一个实例对象,为程序带来了开拓思路;
b.)内存开销可能会占用一定空间,但它避免了每次都需要开辟空间的消费;
c.)使得程序能逻辑清理,开发者可以单一的使用该实例对象去操作,而不别每次都要new一个实例对象来进行操作。
2.缺点:
a.)多个用户在操作这个对象时,线程的安全性,就需要考虑。
b.)一直在占用资源,而不可以被回收。
标签:des style blog http io color os ar 使用
原文地址:http://www.cnblogs.com/yy3b2007com/p/4057300.html