经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
浅谈单例
来源:cnblogs  作者:CDCDDCDC  时间:2019/2/1 11:01:29  对本文有异议

单例模式

1.个人思路

  • 1.业务需求

统一使用同一个对象(创建的对象为一个 使用的对象是同一个)

  • 2.业务思考
  1. 如何生成对象 保存对象
  2. 如何保证生成的对象唯一
  • 3.实现的方案
  1. 利用系统在需要用到对象前进行唯一 一次初始化
  2. 调用对象为空的时候 进行一次初始化 保存对象

2.保证生成的单例唯一

2.1 使用static让系统帮忙初始化

  1. ` public sealed class Singleton
  2. {
  3. private static readonly Singleton instance = new Singleton();
  4. private Singleton() { }
  5. public static Singleton Instance
  6. {
  7. get { return instance; }
  8. }
  9. }`

2.2 使用Unity的生命周期

  1. ` public sealed class Singleton : MonoBehaviour {
  2. public static Singleton Instance { get; private set; }
  3. private void Awake()
  4. {
  5. Instance = this;
  6. }
  7. }`

2.3 使用锁保证单例唯一

  1. ` public sealed class Singleton {
  2. private static Singleton instance = null;
  3. private static readonly Object syuncLock = new Object();
  4. private Singleton() { }
  5. public static Singleton Instance
  6. {
  7. get
  8. {
  9. if (instance == null)
  10. {
  11. lock (syuncLock)
  12. {
  13. if (instance == null)
  14. {
  15. instance = new Singleton();
  16. }
  17. }
  18. }
  19. return instance;
  20. }
  21. }
  22. }`

2.4 使用字典保证生成的单例唯一

  1. `
  2. public sealed class Singleton : ISingleton
  3. {
  4. private Singleton() { }
  5. }
  6. public interface ISingleton {
  7. }
  8. public class SingletonManager
  9. {
  10. private static readonly ConcurrentDictionary<string, ISingleton> singletonDict = new ConcurrentDictionary<string, ISingleton>();
  11. public static T GetSingleton<T>() where T : class, ISingleton
  12. {
  13. string className = typeof(T).ToString();
  14. if (!singletonDict.ContainsKey(className))
  15. {
  16. ConstructorInfo[] constructorInfos = typeof(T).GetConstructors (BindingFlags.Instance | BindingFlags.NonPublic);
  17. ConstructorInfo constructorInfo = Array.Find(constructorInfos, c => c.GetParameters().Length == 0);
  18. if (constructorInfo == null)
  19. {
  20. throw new Exception("生成失败! 没有找到私有的构造函数");
  21. }
  22. T manager = constructorInfo.Invoke (null) as T;
  23. singletonDict.TryAdd(className, manager);
  24. }
  25. return singletonDict[className] as T;
  26. }
  27. }`

2.5 综合考虑

  1. ` public class SingletonManager {
  2. private static GameObject singletonManager;
  3. private static readonly object syuncLock = new object();
  4. private static readonly Dictionary<string, ISingleton> singletonDict = new Dictionary<string, ISingleton>();
  5. public static T GetSingleton<T>() where T : class, ISingleton
  6. {
  7. string className = typeof(T).ToString();
  8. if (!singletonDict.ContainsKey(className))
  9. {
  10. lock (syuncLock)
  11. {
  12. if (!singletonDict.ContainsKey(className))
  13. {
  14. T manager = null;
  15. if (typeof(T).IsSubclassOf(typeof(MonoBehaviour)))
  16. {
  17. AddComponentScript<T>(className);
  18. }
  19. else
  20. {
  21. AddScript(className, manager);
  22. }
  23. }
  24. }
  25. }
  26. return singletonDict[className] as T;
  27. }
  28. private static void AddComponentScript<T>(string className)
  29. {
  30. if(singletonManager == null)
  31. {
  32. singletonManager = new GameObject("singletonManager");
  33. UnityEngine.Object.DontDestroyOnLoad(singletonManager);
  34. }
  35. Type type = typeof(T);
  36. singletonManager.AddComponent(type);
  37. singletonDict.Add(className, singletonManager.GetComponent(type) as ISingleton);
  38. }
  39. private static void AddScript<T>(string className, T manager) where T : class, ISingleton
  40. {
  41. ConstructorInfo[] constructorInfos = typeof(T).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
  42. ConstructorInfo constructorInfo = Array.Find(constructorInfos, c => c.GetParameters().Length == 0);
  43. if (constructorInfo == null)
  44. {
  45. throw new Exception("生成失败! 没有找到私有的构造函数");
  46. }
  47. manager = constructorInfo.Invoke(null) as T;
  48. singletonDict.Add(className, manager);
  49. }
  50. }`
  51. `public interface ISingleton
  52. {
  53. }`

 

原文链接:http://www.cnblogs.com/programmingAdorableNew/p/10344775.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号