博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#单例模式
阅读量:4582 次
发布时间:2019-06-09

本文共 2942 字,大约阅读时间需要 9 分钟。

方法一:只适用于单线程环境,不推荐

将构造函数设置为私有函数以禁止他人创建实例

public sealed class Singleton1    {        private Singleton1()        {        }        private static Singleton1 instance = null;        public static Singleton1 Instance        {            get            {                if (instance == null)                    instance = new Singleton1();                return instance;            }        }    }
View Code

方法二:支持多线程环境,但效率不高,不推荐

public sealed class Singleton2    {        private Singleton2()        {        }        private static readonly object syncObj = new object();        private static Singleton2 instance = null;        public static Singleton2 Instance        {            get            {                lock (syncObj)                {                    if (instance == null)                        instance = new Singleton2();                }                return instance;            }        }    }
View Code

每次通过Instance得到Singleton2的实例,都会试图加上一个锁,而加锁是比较耗时的操作。

方法三:加锁前后判断实例是否存在,代码较复杂

public sealed class Singleton3    {        private Singleton3()        {        }        private static object syncObj = new object();        private static Singleton3 instance = null;        public static Singleton3 Instance        {            get            {                if (instance == null)                {                    lock (syncObj)                    {                        if (instance == null)                            instance = new Singleton3();                    }                }                return instance;            }        }    }
View Code

方法四:使用静态构造函数,推荐

public sealed class Singleton4    {        private Singleton4()        {            Console.WriteLine("An instance of Singleton4 is created.");        }        public static void Print()        {            Console.WriteLine("Singleton4 Print");        }        private static Singleton4 instance = new Singleton4();        public static Singleton4 Instance        {            get            {                return instance;            }        }    }

.NET运行时能够确保只调用一次静态构造函数,C#调用静态构造函数的时机不是由程序员掌控的,而是.NET运行时发现第一次使用一个类型的时候自动调用该类型的静态构造函数。缺点:会过早的创建实例,从而降低内存的使用效率。

方法五:按需创建实例,推荐

public sealed class Singleton5    {        private Singleton5()        {            Console.WriteLine("An instance of Singleton5 is created.");        }        public static void Print()        {            Console.WriteLine("Singleton5 Print");        }        public static Singleton5 Instance        {            get            {                return Nested.instance;            }        }        class Nested        {            static Nested()            {            }            internal static readonly Singleton5 instance = new Singleton5();        }    }

内部定义私有类型Nested,第一次用到这个嵌套类型时会调用静态构造函数创建Singleton5的实例instance。由于Nested为私有的,他人不发使用Nested类型。当不调用Singleton.Instance,就不会触发.NET运行时调用Nested,也不会创建实例,这样就做到了按需创建。

 

转载于:https://www.cnblogs.com/larry-xia/p/10326811.html

你可能感兴趣的文章
JavaScript:值类型 引用类型
查看>>
JavaScript:Function/Object/prototype/__proto__
查看>>
JSP自定义tag
查看>>
Response对象
查看>>
schtasks命令遇见ERROR: The request is not supported.
查看>>
【python】学习笔记10-装饰器
查看>>
Linux 系统下 centOS 7 ipconfig 提示没有安装
查看>>
php多态设计
查看>>
git常用操作
查看>>
锋利的jq第三天
查看>>
CSS控制文本内容固定行行显示,超出部分“...”
查看>>
FormsAuthentication使用指南
查看>>
解压缩文件
查看>>
Maven打包附加配置文件
查看>>
面经二
查看>>
使用css实现特殊标志或图形
查看>>
利用接口实现自定义监听事件以及观察者模式
查看>>
Android------>TableLayout表格布局方式
查看>>
Tesnsorflow命名空间与变量管理参数reuse
查看>>
apply、call、bind有什么区别?
查看>>