using System;

  class SingletonPattern {

  //  Singleton Pattern      Judith Bishop Dec 2006
  //  The public property protects the private constructor
    
    public sealed class Singleton {
      // Private Constructor
      Singleton() { }
      
      // Private object instantiated with private constructor
      static readonly Singleton instance = new Singleton();

      // Public static property to get the object
      public static Singleton UniqueInstance {
        get { return instance;}
      }
    }

    static void Main() {
      Singleton s1 = Singleton.UniqueInstance;
      Singleton s2 = Singleton.UniqueInstance;

      if (s1 == s2) {
        Console.WriteLine("Objects are the same instance");
      }
    }
  }

相关文章:

  • 2021-08-29
  • 2021-10-25
  • 2021-12-01
  • 2021-05-25
猜你喜欢
  • 2021-11-13
  • 2021-09-02
  • 2021-09-29
  • 2021-11-05
  • 2021-07-07
  • 2022-01-28
  • 2021-05-17
相关资源
相似解决方案