经常要用到Singleton模式,通常情况下我们是为那个类写一个static method来创建对象。.net3.5下,可以封装这么一个辅助类,
看代码如下:
1
#region
Singleton
2 /// <summary>
3 /// Used for classes that are single instances per appdomain
4 /// </summary>
5 public static class Singleton
6 {
7 private static class Storage < T >
8 {
9 internal static T s_instance;
10 }
11
12 /// <summary>
13 /// Gets the instance.
14 /// </summary>
15 /// <typeparam name="T"> Func <T></typeparam>
16 /// <param name="op"> The op. </param>
17 /// <returns> T </returns>
18 /// <remark> Author : PetterLiu 2008-12-2816:02 http://wintersun.cnblogs.com </remark>
19 public static T GetInstance < T > (Func < T > op)
20 {
21 if (Storage < T > .s_instance == null )
22 {
23 lock ( typeof (Storage < T > ))
24 {
25 if (Storage < T > .s_instance == null )
26 {
27 T temp = op();
28 System.Threading.Thread.MemoryBarrier();
29 Storage < T > .s_instance = temp;
30 }
31 }
32 }
33 return Storage < T > .s_instance;
34 }
35
36 /// <summary>
37 /// Gets the instance.
38 /// </summary>
39 /// <typeparam name="T"> T </typeparam>
40 /// <returns> T </returns>
41 /// <remark> Author : PetterLiu 2008-12-2816:02 http://wintersun.cnblogs.com </remark>
42 public static T GetInstance < T > ()
43 where T : new ()
44 {
45 return GetInstance(() => new T());
46 }
47 }
48 #endregion
2 /// <summary>
3 /// Used for classes that are single instances per appdomain
4 /// </summary>
5 public static class Singleton
6 {
7 private static class Storage < T >
8 {
9 internal static T s_instance;
10 }
11
12 /// <summary>
13 /// Gets the instance.
14 /// </summary>
15 /// <typeparam name="T"> Func <T></typeparam>
16 /// <param name="op"> The op. </param>
17 /// <returns> T </returns>
18 /// <remark> Author : PetterLiu 2008-12-2816:02 http://wintersun.cnblogs.com </remark>
19 public static T GetInstance < T > (Func < T > op)
20 {
21 if (Storage < T > .s_instance == null )
22 {
23 lock ( typeof (Storage < T > ))
24 {
25 if (Storage < T > .s_instance == null )
26 {
27 T temp = op();
28 System.Threading.Thread.MemoryBarrier();
29 Storage < T > .s_instance = temp;
30 }
31 }
32 }
33 return Storage < T > .s_instance;
34 }
35
36 /// <summary>
37 /// Gets the instance.
38 /// </summary>
39 /// <typeparam name="T"> T </typeparam>
40 /// <returns> T </returns>
41 /// <remark> Author : PetterLiu 2008-12-2816:02 http://wintersun.cnblogs.com </remark>
42 public static T GetInstance < T > ()
43 where T : new ()
44 {
45 return GetInstance(() => new T());
46 }
47 }
48 #endregion
UnitTest:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 /// <summary>
2 /// Commons the test.
3 /// </summary>
4 /// <remark>Author : PetterLiu 2008-12-2816:04 http://wintersun.cnblogs.com </remark>
5 [TestMethod]
6 public void CommonTest()
7 {
8 Pipe pipe1 = Singleton.GetInstance<Pipe>();
9 Pipe pipe2 = Singleton.GetInstance<Pipe>();
10 Assert.AreSame(pipe1, pipe2);
11 }
12
13
14 /// <summary>
15 /// Anonymouses this instance.
16 /// </summary>
17 /// <remark>Author : PetterLiu 2008-12-2816:04 http://wintersun.cnblogs.com </remark>
18 [TestMethod]
19 public void Anonymous()
20 {
21 Pipe pipe1 = Singleton.GetInstance<Pipe>(delegate() { return new Pipe();});
22 Pipe pipe2 = Singleton.GetInstance<Pipe>(delegate() { return new Pipe(); });
23 Assert.AreSame(pipe1,pipe2);
24 }
25
26 /// <summary>
27 /// Usings the lambda.
28 /// </summary>
29 /// <remark>Author : PetterLiu 2008-12-2816:04 http://wintersun.cnblogs.com </remark>
30 [TestMethod]
31 public void UsingLambda()
32 {
33 Pipe pipe1 = Singleton.GetInstance<Pipe>(() => new Pipe());
34 Pipe pipe2 = Singleton.GetInstance<Pipe>(() => new Pipe());
35 Assert.AreSame(pipe1, pipe2);
36 }
http://wintersun.cnblogs.com