单例模式

1.单线程的单例模式

class定义如下:

public class Singleton
    {
        private Singleton()//构建函数私有化,使外部无法直接实例化这个类
        {
            Thread.Sleep(1000);
            Console.WriteLine("{0}被构造,线程id={1}",this.GetType().Name,Thread.CurrentThread.ManagedThreadId);
        }
        private static Singleton _singleton=null;//声明一个类_singleton
        public static Singleton CreateInstance()//给外部一个调用这个类的入口
        {
            if(_singleton==null)//判断该类是否已经实例化,如果没有,则实例化;若是,直接返回该类
            {
                _singleton = new Singleton();
            }
            return _singleton;
        }
        public void show()//测试调用函数
        {
            Console.WriteLine("{0}调用了show",this.GetType().Name);
        }
    }

  Program如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SingletonPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            for(int i = 1; i <= 10; i++)//循环多次调用实例化函数
            {
                Singleton singleton = Singleton.CreateInstance();
                singleton.show();
            }
            Console.ReadLine();
        }
    }
}

  结果:仅实例化一次

    

2.多线程的单例模式(也支持单线程)

  1)多线程Program如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SingletonPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            for(int i = 1; i <= 10; i++)//循环多次调用实例化函数
            {
                new Action(() =>        //使循环变成一个多线程
                {
                    Singleton singleton = Singleton.CreateInstance();
                    singleton.show();
                }).BeginInvoke(null,null);//启动一个异步多线程调用
            }
            Console.ReadLine();
        }
    }
}

 

   2)class定义的三种模式

  双if+lock模式

public class Singleton
    {
        private Singleton()//构建函数私有化,使外部无法直接实例化这个类
        {
            Thread.Sleep(1000);
            Console.WriteLine("{0}被构造,线程id={1}",this.GetType().Name,Thread.CurrentThread.ManagedThreadId);
        }
        private static Singleton _singleton=null;//声明一个类_singleton
        private static object Singleton_lock = new object();
        public static Singleton CreateInstance()//给外部一个调用这个类的入口
        {
            if (_singleton == null)//如果已经实例化,则后续线程不需要再等待锁,提高性能
            {
                lock (Singleton_lock)//保证只有一个线程进入判断
                {
                    Thread.Sleep(1000);
                    if (_singleton == null)//判断该类是否已经实例化,如果没有,则实例化;若是,直接返回该类
                    {
                        _singleton = new Singleton();
                    }
                }
            }
            return _singleton;
        }
        public void show()//测试调用函数
        {
            Console.WriteLine("{0}调用了show",this.GetType().Name);
        }
    }

  静态构造函数模式

public class Singleton
    {
        private Singleton()//构建函数私有化,使外部无法直接实例化这个类
        {
            Thread.Sleep(1000);
            Console.WriteLine("{0}被构造,线程id={1}",this.GetType().Name,Thread.CurrentThread.ManagedThreadId);
        }
        private static Singleton _singleton=null;//声明一个类_singleton
        /// <summary>
        /// 静态构造函数,由CLR保证,函数在被调用前会调用且只调用一次这个静态构造函数
        /// </summary>
        static Singleton ()
        {
            _singleton = new Singleton();
        }

        public static Singleton CreateInstance()//给外部一个调用这个类的入口
        {
            return _singleton;
        }
        public void show()//测试调用函数
        {
            Console.WriteLine("{0}调用了show",this.GetType().Name);
        }
    }

  静态变量模式

    public class Singleton
    {
        private Singleton()//构建函数私有化,使外部无法直接实例化这个类
        {
            Thread.Sleep(1000);
            Console.WriteLine("{0}被构造,线程id={1}",this.GetType().Name,Thread.CurrentThread.ManagedThreadId);
        }
        /// <summary>
        /// 静态变量,由CLR保证,会在这个类第一次使用的时候初始化,且只初始化一次
        /// </summary>
        private static Singleton _singleton=new Singleton ();

        public static Singleton CreateInstance()//给外部一个调用这个类的入口
        {
            return _singleton;
        }
        public void show()//测试调用函数
        {
            Console.WriteLine("{0}调用了show",this.GetType().Name);
        }
    }

 

posted @ 2018-06-03 16:48  wskxy  阅读(137)  评论(0编辑  收藏  举报