Singleton4.cs 864 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace SingletonPattern
  7. {
  8. /// <summary>
  9. /// 通过继承泛型单例来获取实例
  10. /// </summary>
  11. public class Singleton4
  12. {
  13. /// <summary>
  14. /// 非公共无参构造函数,确保该类无法在其他地方实例化
  15. /// </summary>
  16. private Singleton4()
  17. {
  18. }
  19. /// <summary>
  20. /// 也可以通过暴露属性获取实例
  21. /// </summary>
  22. public static Singleton4 Instance
  23. {
  24. get
  25. {
  26. return GenericSingleton<Singleton4>.GetInstance();
  27. }
  28. }
  29. public void GetInfo()
  30. {
  31. Console.WriteLine(string.Format("I am {0}.", this.GetType().Name));
  32. }
  33. }
  34. }