经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
Singleton 单例模式简介与 C# 示例【创建型】【设计模式来了】
来源:cnblogs  作者:橙子家  时间:2023/5/26 9:54:55  对本文有异议

 〇、简介

1、什么是单例模式?

一句话解释:

??单一的类,只能自己来创建唯一的一个对象。

单例模式(Singleton Pattern)是日常开发中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有一个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象

 一个比喻:(班主任与学生)

比如一个班级,只有一个班主任,任何一个同学要找班主任,都是找的同一个,班主任忙的时候,当然就出现排队的情况。

2、优缺点和使用场景

  • 优点:内存里只有一个实例,减少了内存的开销,也避免了对象高频创建带来的性能损耗。
  • 缺点:任务量大时,会出现排队,耗时增加。另外与单一职责原则冲突,一个类应该只关心内部逻辑,而不关心外面怎么样来实例化。

 使用场景举例:

  • 要求生产唯一序列号。
  • WEB 中的计数器,不用每次刷新都在数据库里加一次,用单例先缓存起来。
  • 创建的一个对象需要消耗的资源过多,比如 I/O 与数据库的连接等。

一、单例模式简单实现

  1. public class Singleton
  2. {
  3. private static Singleton instance = null;
  4. private static object lockObject = new object();
  5. /// <summary>
  6. /// 私有化构造函数,防止外部实例化
  7. /// </summary>
  8. private Singleton() { }
  9. public static Singleton Instance
  10. {
  11. get
  12. {
  13. if (instance == null)
  14. {
  15. lock (lockObject) // 线程同步锁
  16. {
  17. if (instance == null) // Lazy Initialization
  18. {
  19. instance = new Singleton();
  20. }
  21. }
  22. }
  23. return instance;
  24. }
  25. }
  26. /// <summary>
  27. /// 重置 Singleton
  28. /// </summary>
  29. public void Reset()
  30. {
  31. instance = null;
  32. }
  33. }

 测试代码:

  1. static void Main(string[] args)
  2. {
  3. var instance1 = Singleton.Instance;
  4. var instance2 = Singleton.Instance;
  5. Console.WriteLine(instance1 == instance2); // 输出 true
  6. }

二、带参数的单例模式实现

  1. public class SingletonParameters
  2. {
  3. private static SingletonParameters instance = null;
  4. private static object lockObject = new object();
  5. private int _firstvalue, _secondvalue;
  6. /// <summary>
  7. /// 私有化构造函数,防止外部实例化
  8. /// </summary>
  9. private SingletonParameters(int first, int second)
  10. {
  11. this._firstvalue = first;
  12. this._secondvalue = second;
  13. }
  14. public static SingletonParameters InstanceParameters(int first, int second)
  15. {
  16. if (instance == null)
  17. {
  18. lock (lockObject) // 线程同步锁
  19. {
  20. if (instance == null) // Lazy Initialization
  21. {
  22. instance = new SingletonParameters(first, second);
  23. }
  24. }
  25. }
  26. else
  27. {
  28. instance.FirstValue = first;
  29. instance.SecondValue = second;
  30. }
  31. return instance;
  32. }
  33. public int FirstValue { get { return _firstvalue; } set { _firstvalue = value; } }
  34. public int SecondValue { get { return _secondvalue; } set { _secondvalue = value; } }
  35. /// <summary>
  36. /// 重置 Singleton
  37. /// </summary>
  38. public void Reset()
  39. {
  40. instance = null;
  41. }
  42. }

 测试代码:

  1. var instance1 = SingletonParameters.InstanceParameters(1, 2);
  2. Console.WriteLine($"FirstValue:{instance1.FirstValue}");
  3. Console.WriteLine($"SecondValue:{instance1.SecondValue}");
  4. var instance2 = SingletonParameters.InstanceParameters(3, 4);
  5. Console.WriteLine($"FirstValue:{instance2.FirstValue}");
  6. Console.WriteLine($"SecondValue:{instance2.SecondValue}");
  7. Console.WriteLine($"instance1 == instance2 : {instance1 == instance2}");

 

参考:https://www.cnblogs.com/gaochundong/p/design_pattern_singleton.html

原文链接:https://www.cnblogs.com/czzj/p/SJMSLL_Singleton.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号