终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

44 rindas
1.2 KiB

  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BPASmartClient.DosingSystem.Service
  8. {
  9. public class ServerFactory
  10. {
  11. private volatile static ServerFactory _Instance;
  12. public static ServerFactory GetInstance => _Instance ?? (_Instance = new ServerFactory());
  13. private ServerFactory() { }
  14. ConcurrentDictionary<string, IAPIServer> servers = new ConcurrentDictionary<string, IAPIServer>();
  15. public void Set<T>(T server) where T : IAPIServer
  16. {
  17. string name = server.GetType().Name;
  18. if (!servers.ContainsKey(name))
  19. {
  20. servers.TryAdd(name, server);
  21. }
  22. }
  23. public IAPIServer Get()
  24. {
  25. if (servers.Count > 0)
  26. return servers.ElementAt(0).Value;
  27. return default;
  28. }
  29. public IAPIServer Get<TServer>(TServer server) where TServer : IAPIServer
  30. {
  31. var name = server.GetType().Name;
  32. if (servers.ContainsKey(name))
  33. return servers[name];
  34. return default;
  35. }
  36. }
  37. }