zoukankan      html  css  js  c++  java
  • 单例模式的两种实现方式:懒汉式 饿汉式

    Code
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace OODemo
    {
        
    /// <summary>
        
    /// 饿汉式单例模式,在第一次加载时就实例化
        
    /// </summary>
        public class Singleton
        {
            
            
    private static readonly Singleton instance=new Singleton();
            
    private Singleton()
            {
                
            }
            
    public static Singleton GetInstance()
            {
                
    return instance;
            }


        }

        
    /// <summary>
        
    /// 懒汉式单例模式,在第一次被引用时开始实例化
        
    /// </summary>
        public class SingletonPattern
        {
            
    private static SingletonPattern instance;
            
    private static object obj;
            
    private SingletonPattern(){}
            
    public static SingletonPattern GetInstance()
            {
                
    if (instance == null)
                {
                    
    lock (obj)
                    {
                        
    if (instance == null)
                        {
                            
    return new SingletonPattern();
                        }
                                           
                    }
                }
            }
        }
            
           
    }
  • 相关阅读:
    基本控件文档-UIView属性---iOS-Apple苹果官方文档翻译
    基本控件文档-UITextField属性---iOS-Apple苹果官方文档翻译
    vue后台管理权限篇
    JavaScript call、apply、bind的用法
    Array map()方法
    markdown常用语法总结
    webpack配置说明
    Object.prototype.toString.call(value)
    前后端数据交互和前端数据展示
    vue常用的传值方式
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/1608283.html
Copyright © 2011-2022 走看看