CalculatorContent.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using SHJX.Service.Control.MoveTime.Interface;
  2. namespace SHJX.Service.Control.MoveTime
  3. {
  4. public class CalculatorContent
  5. {
  6. private static readonly ConcurrentDictionary<string, ICalculator> Calculators = new();
  7. private static CalculatorContent _content;
  8. private static readonly object locker = new();
  9. private CalculatorContent() { }
  10. public static CalculatorContent Instance
  11. {
  12. get
  13. {
  14. if (_content is null)
  15. {
  16. lock (locker)
  17. {
  18. _content = new();
  19. }
  20. }
  21. return _content;
  22. }
  23. }
  24. public ICalculator Factory(string step)
  25. {
  26. if (Calculators.TryGetValue(step, out ICalculator value)) return value;
  27. string className = CreatQueryClassName(step);
  28. try
  29. {
  30. ICalculator calculator = Activator.CreateInstance(Type.GetType(className), true) as ICalculator;
  31. Calculators.TryAdd(step, calculator);
  32. return calculator;
  33. }
  34. catch
  35. {
  36. throw new ArgumentNullException(step);
  37. }
  38. }
  39. private string CreatQueryClassName(string step) => $"{GetType().Namespace}.Handler.{step}Calculator";
  40. }
  41. }