using System;

  //  Singleton PatternJudith Bishop Nov 2007
  //  Generic version
 
  public class Singleton <T>  where T : class, new(){
     Singleton() { }
 
    class SingletonCreator {
      static SingletonCreator () {}
      // Private object instantiated with private constructor
      internal static readonly T instance = new T();
    }

    public static T UniqueInstance {
      get {return SingletonCreator.instance;}
    }
  }

  class Test1 {}
  class Test2 {}

  class Client {

    static void Main () {
      Test1 t1a = Singleton<Test1>.UniqueInstance;
      Test1 t1b = Singleton<Test1>.UniqueInstance;
      Test2 t2 = Singleton <Test2>.UniqueInstance;

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

相关文章:

  • 2021-06-30
  • 2021-09-10
  • 2021-11-22
  • 2021-08-21
  • 2021-08-16
  • 2022-01-16
猜你喜欢
  • 2022-01-08
  • 2022-12-23
  • 2021-11-12
  • 2021-08-27
  • 2021-06-13
  • 2021-12-22
  • 2021-11-02
相关资源
相似解决方案