• Singleton


    using System;

    public sealed class Singleton1
    {
     static Singleton1 instance;
     Singleton1(){}
     public static Singleton1 Instance
     {
      get{
      if (instance == null)
      instance = new Singleton1();
      return instance;
     }
     }
    }

    public sealed class Singleton2
    {
     static Singleton2 instance;
     static readonly object padlock = new object();
     
     public static Singleton2 Instance
     {
      get
      {
       lock (padlock)
       {
        if (instance == null)
        instance = new Singleton2();
       }
       return instance;
      }
     }
    }

    public sealed class Singleton3
    {
     static Singleton3 instance;
     static readonly object padlock = new object();
     public static Singleton3 Instance
     {
      get{
      if (instance == null)
      lock(padlock){
      if (instance == null){
      instance = new Singleton3();
     }
    }
           return instance;
    }
     }
    }

    public sealed class Singleton4
    {
     static readonly Singleton4 instance = new Singleton4();
     static Singleton4(){}
     Singleton4(){}
     
     public static Singleton4 Instance
     {
      get{
      return instance;
     }
    }
    }

    public sealed class Singleton5
    {
     Singleton5(){}
     
     public static Singleton5 Instance
     {
      get{
      return Nested.instance;
     }
    }
     
     class Nested{
     static Nested(){}
     internal static readonly Singleton5 instance = new Singleton5();
     }
    }

  • 相关阅读:
    linux历史及基本知识
    this关键字
    类加载与对象初始化
    学习之vim
    简单的登录界面(包括帐号密码验证)
    Web前端基础
    九大排序算法
    对“面向对象”思想的理解
    交换机
    网络编程协议详解
  • 原文地址:https://www.cnblogs.com/archmaster/p/118899.html
Copyright © 2020-2023  润新知