单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该实例的全局访问点。
一、常用模式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public
class
Singleton
{
private
static
Singleton instance;
private
Singleton()
{
}
public
static
Singleton GetInstance()
{
if
(instance==
null
)
{
instance=
new
Singleton();
}
return
instance;
}
}
|
这种模式只适用在单线程下,并没有考虑到线程并发的问题,在多线程中,可能出现两个线程同时获取Instance的实例,可能会出现多个实例对象。
二、多线程下Lazy模式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public
class
Singleton
{
private
static
Singleton instance;
private
static
object
_lock=
new
object
();
private
Singleton()
{
}
public
static
Singleton GetInstance()
{
if
(instance==
null
)
{
lock
(_lock)
{
if
(instance==
null
)
{
instance=
new
Singleton();
}
}
}
return
instance;
}
}
|
使用了Lock锁,可以较好的解决多线程下单例模式的实现,lock锁保证同一时间内只有一个线程能访问Lock里面的语句块,比较有效的解决性能的开销。
三、饿汉模式
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public
sealed
class
Singleton
{
private
static
readonly
Singleton instance=
new
Singleton();
private
Singleton()
{
}
public
static
Singleton GetInstance()
{
return
instance;
}
}
|
该类标记为 sealed 以阻止发生派生,而派生可能会增加实例。使用的readonly关键可以跟static一起使用,用于指定该常量是类别级的,它的初始化交由静态构造函数实现,并可以在运行时编译。在这种模式下,无需自己解决线程安全性问题,CLR会给我们解决。由此可以看到这个类被加载时,会自动实例化这个类,而不用在第一次调用GetInstance()后才实例化出唯一的单例对象。
四、延迟加载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public
sealed
class
Singleton
{
Singleton()
{
}
public
static
Singleton Instance
{
get
{
return
Nested.instance;
}
}
class
Nested
{
static
Nested()
{
}
internal
static
readonly
Singleton instance =
new
Singleton();
}
}
|
这种方法用的比较的少,但是也是一种不错的方法。