Singleton Pattern 单件模式
这次公司讨论学习了Singleton Pattern。提供了几种实现方式:
1
public class Singleton
2

{
3
private static Singleton instance;
4
private Singleton()
{}
5
public static Singleton Instance
6
{
7
get
8
{
9
if (instance == null)
10
{
11
instance = new Singleton();
12
}
13
return instance;
14
}
15
}
16
}
17
public class Singleton 2


{ 3
private static Singleton instance; 4

private Singleton()
{} 5
public static Singleton Instance 6

{ 7
get 8

{ 9
if (instance == null) 10

{ 11
instance = new Singleton(); 12
} 13
return instance; 14
} 15
} 16
} 17

缺点:由于初始化实例只执行一次,因此,if语句总是要进行判断,这种实现并不太好。
2 静态初始化
using System;
public sealed class Singleton


{
private static readonly Singleton instance = new Singleton();

private Singleton()
{}
public static Singleton Instance

{
get

{
return instance;
}
}
} 优点:没有1中实现的if语句问题。
缺点:1,无法延迟实现 2 ,无法继承,也就是无法实现需要n(n>1)个实例的需求。
3 延迟实现
public sealed class Singleton


{
Singleton()

{
}

public static Singleton GetInstance()

{
return SingletonCreator.instance;
}
class SingletonCreator

{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static SingletonCreator()

{
}

internal static readonly Singleton instance = new Singleton();
}
}
4 多线程
using System;
public sealed class Singleton


{
private static volatile Singleton instance;
private static object syncRoot = new Object();

private Singleton()
{}
public static Singleton Instance

{
get

{
if (instance == null)

{
lock (syncRoot)

{
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
}


5 泛型实现
using System;
public sealed class Singleton


{
private static volatile Singleton instance;
private static object syncRoot = new Object();

private Singleton()
{}
public static Singleton Instance

{
get

{
if (instance == null)

{
lock (syncRoot)

{
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
}


6 MonoState 实现单一行为
public class Monostate


{
private static int x = 0;

public Monostate()
{}

public int X

{
get

{
return x;
}
set

{
x = value;
}
}
}


2 静态初始化
using System;
public sealed class Singleton 

{
private static readonly Singleton instance = new Singleton(); 
private Singleton()
{}
public static Singleton Instance 
{
get 
{
return instance;
}
}
} 缺点:1,无法延迟实现 2 ,无法继承,也就是无法实现需要n(n>1)个实例的需求。

public sealed class Singleton

{
Singleton()
{
}
public static Singleton GetInstance()
{
return SingletonCreator.instance;
}
class SingletonCreator
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static SingletonCreator()
{
}
internal static readonly Singleton instance = new Singleton();
}
}4 多线程
using System;
public sealed class Singleton 

{
private static volatile Singleton instance;
private static object syncRoot = new Object(); 
private Singleton()
{}
public static Singleton Instance 
{
get 
{
if (instance == null) 
{
lock (syncRoot) 
{
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
}

5 泛型实现
using System;
public sealed class Singleton 

{
private static volatile Singleton instance;
private static object syncRoot = new Object(); 
private Singleton()
{}
public static Singleton Instance 
{
get 
{
if (instance == null) 
{
lock (syncRoot) 
{
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
}

6 MonoState 实现单一行为
Singleton强制结构上的单一性,防止创建出多个实例对象。
Montstate强制行为上的单一性,强制不同对象中的同一个属性在同一时间必须有相同的值。
Monostate模式的好处
•透明性:表面上看没有特别的地方。不需要特殊的实现。
•可派生:派生类和基类共享相同的静态变量。
public class Monostate

{
private static int x = 0;
public Monostate()
{}
public int X 
{
get 
{
return x;
}
set 
{
x = value;
}
}
}

测试代码:
using System;
using System.Collections.Generic;
using System.Text;
namespace TestStaticInMultiThread

{
class Program
{
static void Main(string[] args)
{
Test t = new Test("t");
Test t2 = new Test("t2");
System.Threading.Thread a = new System.Threading.Thread(new
System.Threading.ThreadStart(t.A));
System.Threading.Thread b = new System.Threading.Thread(new
System.Threading.ThreadStart(t2.A));
Console.WriteLine("New Thread a start");
a.Start();
Console.WriteLine("New Thread a end");
Console.WriteLine("New Thread b start");
b.Start();
Console.WriteLine("New Thread b end");
//t.A();
//t2.A();
Console.ReadLine();
}
}
class Test
{
static int x = 0;
string s;
public Test(string str)
{ s = str; }
public void A()
{
x++;
Console.WriteLine("{1}- A :{0}", x,s);
B();
}
private void B()
{
x++;
Console.WriteLine("{1}-B : {0}", x,s);
C();
}
private void C()
{
x++;
Console.WriteLine("{1}- C : {0}", x,s);
D();
}
private void D()
{
x++;
Console.WriteLine("{1}- D : {0}", x,s);
}
}
}
8 Singleton Pattern 涉及到的知识点
static 关键字 [C#]
readonly 关键字 [C#]
sealed 关键字 [C#]
序列化
ICloneable 接口
volatile 关键字 [C#]
lock 关键字 [C#]
Lazy Load
Double-checked locking

浙公网安备 33010602011771号