ibcadmin 发表于 2019-11-8 09:49:44

c#设计模式之单例模式

<p><strong>c#计划模式之单例模式</strong></p>
<p style="background-color: darkcyan; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>场景形貌</strong></p>
<p style="margin-left: 30px; line-height: 25px;">单例模式对于我们来说一点也不模式,是一个常见的名称,单例模式在步调中的实际效果就是:确保一个步调中只有一个实例,并提供一个全局访问点,节省系统资源</p>
<p style="margin-left: 30px; line-height: 25px;">单例模式无论是在实际开发中还是在软件应用中比力常见,比如,windows系统的使命管理器、IIS的HttpApplication、实际项目中的日记组件等等</p>
<p style="background-color: darkcyan; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>实现方式</strong></p>
<p style="width: 100%; line-height: 30px;">   单例模式为了实现一个实例,那么只有不把实例创建袒暴露去,只通过类自己来创建实例,为了实现效果,必要界说一个私有构造函数</p>
<p style="width: 100%; line-height: 30px;">  单例模式实现方式有:饿汉式、懒汉式、双重验证式、静态内部类、耽误加载(Lazy)</p>
<p style="width: 100%; line-height: 30px;">  下面分别对每一种实现方式做一个简单的实例,以及其优缺点</p>
<p style="background-color: deepskyblue; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>  饿汉式</strong></p>

   /// <summary>
    /// 创建一个 Singleton 类(饿汉式)
    /// 这种方式比力常用,但容易产生垃圾对象。
    ///优点:没有加锁,实验效率会进步。
    ///缺点:类加载时就初始化,浪费内存。
    ///它基于 classloder 机制避免了多线程的同步标题,不外,instance 在类装载时就实例化,
    ///固然导致类装载的缘故因由有许多种,在单例模式中大多数都是调用 getInstance 方法,
    ///但是也不能确定有其他的方式(或者其他的静态方法)导致类装载,这时间初始化 instance 显然没有到达 lazy loading 的效果。
    /// </summary>
    public class SingleObject
    {
      //创建 SingleObject 的一个对象
      private static SingleObject instance = new SingleObject();

      //让构造函数为 private,如许该类就不会被实例化
      private SingleObject() {
            Console.WriteLine("我被创建了.饿汉式");
      }

      //获取唯一可用的对象
      public static SingleObject GetInstance()
      {
            return instance;
      }

      public void ShowMessage()
      {
            Console.WriteLine("Hello World.饿汉式");
      }
    }

<p> </p>
<p style="background-color: deepskyblue; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>  懒汉式</strong></p>
<p style="width: 100%; line-height: 30px;">   <strong> </strong></p>

/// <summary>
    /// 创建一个 Singleton 类(懒汉式)
    /// 这种方式具备很好的 lazy loading,可以或许在多线程中很好的工作,但是,效率很低,99% 环境下不必要同步。
    /// 优点:第一次调用才初始化,避免内存浪费。
    /// 缺点:懒汉式在单个线程中没有标题,但多个线程同事访问的时间就大概同事创建多个实例,而且这多个实例不是同一个对象。
    /// </summary>
    public class SingleObject1
    {
      //创建 SingleObject 的一个对象
      private static SingleObject1 instance;

      //让构造函数为 private,如许该类就不会被实例化
      private SingleObject1() { }

      //获取唯一可用的对象
      public static SingleObject1 GetInstance()
      {
            if (instance == null)
            {
                instance = new SingleObject1();
                Console.WriteLine("我被创建了.懒汉式");
            }
            return instance;
      }

      public void ShowMessage()
      {
            Console.WriteLine("Hello World.懒汉式");
      }
    }

<p> </p>
<p style="background-color: deepskyblue; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>  双重验证式</strong></p>
<p style="width: 100%; line-height: 30px;"> </p>

    /// <summary>
    /// 创建一个 Singleton 类(双重验证)
    /// 这种方式具备很好的 lazy loading,可以或许在多线程中很好的工作,但是,效率很低,99% 环境下不必要同步。
    /// 优点:第一次调用才初始化,避免内存浪费,线程安全。
    /// 缺点:必须加锁 synchronized 才华包管单例,但加锁会影响效率。
    /// </summary>
    public class SingleObject2
    {
      //创建 SingleObject 的一个对象
      private static SingleObject2 instance;

      // 界说一个标识确保线程同步
      private static readonly object locker = new object();


      //让构造函数为 private,如许该类就不会被实例化
      private SingleObject2() { }

      //获取唯一可用的对象
      public static SingleObject2 GetInstance()
      {
            //// 假如为空,那么就加锁,创建实例
            if (instance == null)
            {
                lock (locker)
                {
                  //// 枷锁乐成后,在做一次非空判定,避免在加锁期间以创建了实例而导致重复创建
                  if (instance == null)
                  {
                        instance = new SingleObject2();
                        Console.WriteLine("我被创建了.双重验证");
                  }
                }
            }
            return instance;
      }

      public void ShowMessage()
      {
            Console.WriteLine("Hello World.双重验证");
      }
    }

<p> </p>
<p style="background-color: deepskyblue; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>  静态内部类</strong></p>
<p style="width: 100%; line-height: 30px;">   </p>

/// <summary>
    /// 创建一个 Singleton 类(静态内部类)
    /// 这种方式不消加锁,在效率上和内存利用上都比力良好
    /// 降服了饿汉模式的不敷饿汉模式实验效率高,由于在类加载的时间初始化导致内存浪费
    /// </summary>
    public class SingletonStatic
    {
      /// <summary>
      /// 内部类
      /// </summary>
      public class SingletonStaticInner
      {
            /// <summary>
            /// 当一个类有静态构造函数时,它的静态成员变量不会被beforefieldinit修饰
            /// 就会确保在被引用的时间才会实例化,而不是步调启动的时间实例化
            /// </summary>
            static SingletonStaticInner() { }

            /// <summary>
            /// 实例化
            /// </summary>
            internal static SingletonStatic singletonStatic = new SingletonStatic();
      }

      /// <summary>
      /// 私有构造函数
      /// </summary>
      private SingletonStatic() {
            Console.WriteLine("我被创建了.静态内部类");
      }

      /// <summary>
      /// 获取实例
      /// </summary>
      /// <returns></returns>
      public static SingletonStatic GetInstance()
      {
            return SingletonStaticInner.singletonStatic;
      }

      public void ShowMessage()
      {
            Console.WriteLine("Hello World.静态内部类");
      }
    }

<p> </p>
<p style="background-color: deepskyblue; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>  耽误加载(Lazy)</strong></p>
<p>   </p>


    /// <summary>
    /// 创建一个 Singleton 类(Lazy)
    /// 该方式是必要.netformwork4+
    /// </summary>
    public class SingletonLazy
    {
      private static Lazy<SingletonLazy> singletonLazy = new Lazy<SingletonLazy>(()=>new SingletonLazy());

      /// <summary>
      /// 私有构造函数
      /// </summary>
      private SingletonLazy()
      {
            Console.WriteLine("我被创建了.Lazy");
      }

      /// <summary>
      /// 获取实例
      /// </summary>
      /// <returns></returns>
      public static SingletonLazy GetInstance()
      {
            return singletonLazy.Value;
      }

      public void ShowMessage()
      {
            Console.WriteLine("Hello World.Lazy");
      }
    }


<p> </p>
<p style="background-color: deepskyblue; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>  每一种创建方式测试</strong></p>
<p style="width: 100%; line-height: 30px;">   创建一个控制台步调,通过多线程对每一种实现方式利用,检察实在例次数分析:</p>

/*
先容
意图:包管一个类仅有一个实例,并提供一个访问它的全局访问点。
告急解决:一个全局利用的类频繁地创建与烧毁。
何时利用:当您想控制实例数目,节省系统资源的时间。
怎样解决:判定系统是否已经有这个单例,假如有则返回,假如没有则创建。
关键代码:构造函数是私有的。
应用实例:
范例的已有应用:
1、windows的使命管理器等
2、IIS的HttpApplication,全部的HttpModule都共享一个HttpApplication实例
在项目中的实际利用场景:
1、日记组件
2、多线程线程池管理
3、网站计数器
4、设置文件管理
   */

class Program
    {
      static void Main(string[] args)
      {
            TaskFactory taskFactory = new TaskFactory();
            List<Task> taskList = new List<Task>();

            //// 测试--饿汉式
            for (int i = 0; i < 5; i++)
            {
                taskList.Add(taskFactory.StartNew(() =>
                {
                  SingleObject.GetInstance();
                }));
            }

            //// 测试--懒汉式
            for (int i = 0; i < 5; i++)
            {
                taskList.Add(taskFactory.StartNew(() =>
                {
                  SingleObject1.GetInstance();
                }));
            }

            //// 测试--双重验证
            for (int i = 0; i < 5; i++)
            {
                taskList.Add(taskFactory.StartNew(() =>
                {
                  SingleObject2.GetInstance();
                }));
            }

            //// 测试--静态内部类
            for (int i = 0; i < 5; i++)
            {
                taskList.Add(taskFactory.StartNew(() =>
                {
                  SingletonStatic.GetInstance();
                }));
            }

            //// 测试--Lazy
            for (int i = 0; i < 5; i++)
            {
                taskList.Add(taskFactory.StartNew(() =>
                {
                  SingletonLazy.GetInstance();
                }));
            }

            Console.ReadLine();
      }
    }

<p> </p>

<p>运行效果:</p>
<p>  </p>
<p> </p>

<p style="background-color: darkcyan; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>总结</strong></p>
<p style="width: 100%; line-height: 30px;">  根据单例模式是每一种实现方式对比分析,在实际利用过程中:发起接纳<strong>耽误加载(Lazy)</strong></p>
<p style="width: 100%; line-height: 30px;"> 固然,尚有其他雷同的实现单例的方式,没有写到的,也接待大家一起互换,勿喷</p>
<p style="width: 100%; line-height: 30px;"> </p>
页: [1]
查看完整版本: c#设计模式之单例模式