单例设计模式(C#) 简单模式

使用场景

来着(http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton)  

  • controls concurrent access to a shared resource.
  • access to the resource will be requested from multiple, disparate parts of the system.
  • there can be only one object.

简单模式

  • 代码

    
    public class BaseSingle
    {
        public void Method()
        {
            Console.WriteLine("{1}.Method:{0}", this.GetHashCode(),this.GetType().ToString());
       }
    }
    
    public class UnsafeSingle : BaseSingle
    {
    
        private static UnsafeSingle _single;
    
        public static UnsafeSingle UnsafeSingleInstatce()
        {
          if (null == _single)
            _single = new UnsafeSingle();
    
          return _single;
        }
    
        private UnsafeSingle()
        {
          Console.WriteLine(DateTime.Now.ToString() + ": 构造函数!");
        }
    
    }
  • 测试代码

    internal delegate void InstanceFun();
    
    public void GetInstatnce()
    {
      UnsafeSingle.UnsafeSingleInstatce().Method();
    }
    public void MutilThreadTest(InstanceFun instanceFun)
    {
        var started = DateTime.Now;
        Thread[] threads = new Thread[100];
    
        for (var index = 0; index < 100; index++)
        {
            Thread thread = new Thread(new ThreadStart(instanceFun));
            threads[index] = thread;
        }
    
        foreach (var thread in threads)
        {
            thread.Start();
        }
        Console.WriteLine("运行时长:{0}", (DateTime.Now - started).Milliseconds);
    }
    
    /// <summary>
    /// 一般单例,非线程安全
    /// </summary>
    [Test]
    public void MutilUnSafeTest()
    {
        MutilThreadTest(GetInstatnce);
    
    }
  • 缺点:对象未创建时,多线程同时访问返回的对象并非一个;

  • 测试结果:   

    DesignPattern.UnsafeSingle.Method:45338574
    DesignPattern.UnsafeSingle.Method:30944461
    DesignPattern.UnsafeSingle.Method:62452338
    DesignPattern.UnsafeSingle.Method:43921088
    DesignPattern.UnsafeSingle.Method:18573725
    DesignPattern.UnsafeSingle.Method:8935080
    DesignPattern.UnsafeSingle.Method:48823647
    DesignPattern.UnsafeSingle.Method:28613802
    DesignPattern.UnsafeSingle.Method:40642054
    DesignPattern.UnsafeSingle.Method:52962918
    DesignPattern.UnsafeSingle.Method:29095347
    DesignPattern.UnsafeSingle.Method:62694277
    DesignPattern.UnsafeSingle.Method:54643837
    DesignPattern.UnsafeSingle.Method:36302867
    DesignPattern.UnsafeSingle.Method:49913678
    DesignPattern.UnsafeSingle.Method:52482287
    DesignPattern.UnsafeSingle.Method:41829035
    DesignPattern.UnsafeSingle.Method:57978397
    DesignPattern.UnsafeSingle.Method:13456355
    DesignPattern.UnsafeSingle.Method:23736788
    DesignPattern.UnsafeSingle.Method:42195492
    DesignPattern.UnsafeSingle.Method:28113429
    DesignPattern.UnsafeSingle.Method:2875151
    DesignPattern.UnsafeSingle.Method:56858914
    DesignPattern.UnsafeSingle.Method:2735313
    DesignPattern.UnsafeSingle.Method:50717245
    DesignPattern.UnsafeSingle.Method:30697644
    DesignPattern.UnsafeSingle.Method:47515266
    DesignPattern.UnsafeSingle.Method:30183720
    DesignPattern.UnsafeSingle.Method:65195544
    DesignPattern.UnsafeSingle.Method:22695823
    DesignPattern.UnsafeSingle.Method:8561069
    DesignPattern.UnsafeSingle.Method:33135541
    DesignPattern.UnsafeSingle.Method:17719913
    DesignPattern.UnsafeSingle.Method:32493892
    DesignPattern.UnsafeSingle.Method:57339132
    DesignPattern.UnsafeSingle.Method:38516275
    DesignPattern.UnsafeSingle.Method:33858004
    DesignPattern.UnsafeSingle.Method:58042889

    DesignPattern.UnsafeSingle.Method:58042889
    DesignPattern.UnsafeSingle.Method:58042889
    DesignPattern.UnsafeSingle.Method:58042889
    DesignPattern.UnsafeSingle.Method:58042889
    DesignPattern.UnsafeSingle.Method:58042889
    ……
    运行时长:115
    DesignPattern.UnsafeSingle.Method:58042889

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值