zoukankan      html  css  js  c++  java
  • 三种单例模式demo

     1

    using System;

    public class Singleton
    {
       private static Singleton instance;

       private Singleton() {}

       public static Singleton Instance
       {
          get 
          {
             if (instance == null)
             {
                instance = new Singleton();
             }
             return instance;
          }
       }
    }

    2


    public sealed class Singleton
    {
       private static readonly Singleton instance = new Singleton();
       
       private Singleton(){}

       public static Singleton Instance
       {
          get 
          {
             return instance; 
          }
       }
    }

    3

    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;
          }
       }
    }
  • 相关阅读:
    操作系统01_进程和线程管理
    数据库02_字段类型
    鲁滨逊漂流记游戏
    查找数N二进制中1的个数(JS版 和 Java版)
    js中的call、apply
    jQuery对象与Dom对象的相互转换
    jndi配置数据源
    关于JS中变量的作用域-实例
    重写equals()方法时,需要同时重写hashCode()方法
    String与StringBuilder
  • 原文地址:https://www.cnblogs.com/cr7/p/2294053.html
Copyright © 2011-2022 走看看