C#版的6种单件实现

C#版的6种单件实现:
1 简单实现:


 1public 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 实现单一行为

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


public class Monostate
{
    private static int x = 0;
    public Monostate()    {}

    public int X
    {
        get
        {
            return x;
        }
        set
        {
            x = value;
        }
    }
}


7 Static 关键字与同一进程下的线程无关
测试代码:

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);
        }
    }
}

测试结果是,静态变量x的值是由1-8,如果加上那个注释语句是1-16。这说明,线程对同一个对象x进行了操作。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值