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

    一般用法

    public sealed class Singleton
    {
    	private static readonly Singleton _instance = new Singleton();
    
    	static Singleton() { }
    	private Singleton() { }
    	public static Singleton Instance
    	{
    		get { return _instance; }
    	}
    }
    

    例外1:需要在不触发初始化的情况下调用其他静态方法;
    例外2:需要知道是否已经实例化了。

    内部类

    /// <summary>
    /// 内部类,完全延迟实例化
    /// </summary>
    public sealed class Singleton2
    {
    	private Singleton2() { }
    	public static Singleton2 Instance { get { return Nested._instance; } }
    
    	private class Nested
    	{
    		static Nested() { }
    		internal static readonly Singleton2 _instance = new Singleton2();
    	}
    }
    

    .NET 4 (or higher)

    /// <summary>
    ///  推荐使用单例
    /// </summary>
    public sealed class Singleton3
    {
    	private static readonly Lazy<Singleton3> _lazy =
    		new Lazy<Singleton3>(() => new Singleton3());
    	private Singleton3() { }
    
    	public static Singleton3 Instance { get { return _lazy.Value; } }
    }
    
  • 相关阅读:
    jsp 生成静态页面
    flash cs5
    sql 设置主键
    sql 分页查询
    java 获取系统时间
    android 源码 编译
    android 退出 activity
    广告平台
    android 引擎
    maya 花草 制作动画
  • 原文地址:https://www.cnblogs.com/wesson2019-blog/p/14304801.html
Copyright © 2011-2022 走看看