设计模式Singleton的Provider实现

SingletonProvider 实现代码

 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Text;
 4 None.gif using  System.Reflection;
 5 None.gif
 6 None.gif namespace  AIO.DesignPattern.Singleton
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8InBlock.gif  public sealed class SingletonProvider<T> where T : class
 9ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
10InBlock.gif    static SingletonProvider()
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12ExpandedSubBlockEnd.gif    }

13InBlock.gif
14InBlock.gif    public static T Instance
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16InBlock.gif      get
17ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
18InBlock.gif        if (_Instance == null)
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20InBlock.gif          //_Instance = Activator.CreateInstance<T>();
21InBlock.gif          _Instance = typeof(T).InvokeMember(typeof(T).Name, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, nullnullnullas T;
22ExpandedSubBlockEnd.gif        }

23InBlock.gif        return _Instance;
24ExpandedSubBlockEnd.gif      }

25ExpandedSubBlockEnd.gif    }

26InBlock.gif
27InBlock.gif    private static T _Instance;
28ExpandedSubBlockEnd.gif  }

29InBlock.gif
30ExpandedBlockEnd.gif}

31 None.gif

取消采用第20行是因为'Activator.CreateInstance<T>()'调用必须实现Public的构造函数

采用现在方式有两种好处:一是将构造函数声明为私有或受保护避免多种方式调用,另一种方式是将构造函数声明为公有使得既可以单件又可以多件使用(本人觉得有时候很有用,尤其是代码作为类库时)

线程安全版本代码如下:

 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Text;
 4 None.gif using  System.Reflection;
 5 None.gif
 6 None.gif namespace  AIO.DesignPattern.Singleton
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8InBlock.gif  public sealed class ThreadSafeSingletonProvider<T> where T : class
 9ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
10InBlock.gif    static ThreadSafeSingletonProvider()
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12ExpandedSubBlockEnd.gif    }

13InBlock.gif
14InBlock.gif    static private object syncObj = new object();
15InBlock.gif
16InBlock.gif    public static T Instance
17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
18InBlock.gif      get
19ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
20InBlock.gif        lock (syncObj)
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif          if (_Instance == null)
23ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
24InBlock.gif            //_instance = Activator.CreateInstance<T>();
25InBlock.gif            _Instance = typeof(T).InvokeMember(typeof(T).Name, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, nullnullnullas T;
26ExpandedSubBlockEnd.gif          }

27InBlock.gif          return _Instance;
28ExpandedSubBlockEnd.gif        }

29ExpandedSubBlockEnd.gif      }

30ExpandedSubBlockEnd.gif    }

31InBlock.gif
32InBlock.gif    private static T _Instance;
33ExpandedSubBlockEnd.gif  }

34ExpandedBlockEnd.gif}

35 None.gif

 

单元测试代码如下:

 1 None.gif using  Microsoft.VisualStudio.TestTools.UnitTesting;
 2 None.gif using  System;
 3 None.gif using  System.Text;
 4 None.gif using  System.Collections.Generic;
 5 None.gif using  AIO.DesignPattern.Singleton;
 6 None.gif namespace  AIO.UnitTest
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8InBlock.gif    [TestClass()]
 9InBlock.gif    public class SingletonProviderTest
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11InBlock.gif        public class SingleInstanceClass
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            public SingleInstanceClass()
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                _count++;
16ExpandedSubBlockEnd.gif            }

17InBlock.gif
18InBlock.gif            public static SingleInstanceClass Instance
19ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
20InBlock.gif                get
21ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
22InBlock.gif                    return SingletonProvider<SingleInstanceClass>.Instance;
23ExpandedSubBlockEnd.gif                }

24ExpandedSubBlockEnd.gif            }

25InBlock.gif
26InBlock.gif            public static int Count
27ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
28InBlock.gif                get
29ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
30InBlock.gif                    return _count;
31ExpandedSubBlockEnd.gif                }

32ExpandedSubBlockEnd.gif            }

33InBlock.gif
34InBlock.gif            public static void DoStatic()
35ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
36ExpandedSubBlockEnd.gif            }

37InBlock.gif
38InBlock.gif            public void DoInstance()
39ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
40ExpandedSubBlockEnd.gif            }

41InBlock.gif
42InBlock.gif            private static int _count = 0;
43ExpandedSubBlockEnd.gif        }

44InBlock.gif
45ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
46InBlock.gif        /// SingletonProvider  Test
47ExpandedSubBlockEnd.gif        /// </summary>

48InBlock.gif        [TestMethod()]
49InBlock.gif        public void InstanceTest()
50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
51InBlock.gif            Assert.AreEqual(SingleInstanceClass.Count, 0);
52InBlock.gif
53InBlock.gif            SingleInstanceClass.DoStatic();
54InBlock.gif            Assert.AreEqual(SingleInstanceClass.Count, 0);
55InBlock.gif
56InBlock.gif            SingleInstanceClass.Instance.DoInstance();
57InBlock.gif            Assert.AreEqual(SingleInstanceClass.Count, 1);
58InBlock.gif
59InBlock.gif            SingleInstanceClass.DoStatic();
60InBlock.gif            Assert.AreEqual(SingleInstanceClass.Count, 1);
61InBlock.gif
62InBlock.gif            SingleInstanceClass.Instance.DoInstance();
63InBlock.gif            Assert.AreEqual(SingleInstanceClass.Count, 1);
64InBlock.gif
65InBlock.gif            SingleInstanceClass.DoStatic();
66InBlock.gif            Assert.AreEqual(SingleInstanceClass.Count, 1);
67ExpandedSubBlockEnd.gif        }

68ExpandedSubBlockEnd.gif    }

69ExpandedBlockEnd.gif}

70 None.gif

 

应用代码简单到只有一行如下:

SingletonProvider<SomeClass>.Instance.doSomething();

ok 今天就到这里 有时间会将其扩展实现单件/多件扩展的 Provide(Pooling FixedSize MaxSize etc.)

If you have all DesignPattern's Provider, you'll look the technology is just simple!

the Art of programing

转载于:https://www.cnblogs.com/Bolik/archive/2006/05/10/396226.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值