Guest User

Untitled

a guest
Apr 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. public abstract class InterestRate {
  2.  
  3. // irrelevant details
  4.  
  5. public static T ImpliedRate<T>(
  6. double factor,
  7. double time,
  8. DayCounter dayCounter
  9. ) where T : NonCompoundedInterestRate {
  10. MethodInfo methodInfo = typeof(T).GetMethod(
  11. "ImpliedRate",
  12. BindingFlags.Static);
  13. return (T)methodInfo.Invoke(
  14. null,
  15. new object[] { factor, time, dayCounter }
  16. );
  17. }
  18.  
  19. public static T ImpliedRate<T>(
  20. double factor,
  21. double time,
  22. DayCounter dayCounter,
  23. Frequency frequency
  24. ) where T : CompoundedInterestRate {
  25. MethodInfo methodInfo = typeof(T).GetMethod(
  26. "ImpliedRate",
  27. BindingFlags.Static);
  28. return (T)methodInfo.Invoke(
  29. null,
  30. new object[] { factor, time, dayCounter, frequency }
  31. );
  32. }
  33.  
  34. public static class InstanceMap
  35. {
  36. private static readonly Dictionary<Type,object> instances =
  37. new Dictionary<Type,object>();
  38.  
  39. public static void AddInstance(object instance)
  40. {
  41. instances[instance.GetType()] = instance;
  42. }
  43.  
  44. public static T GetInstance<T>() { return (T) instances[typeof(T)]; }
  45. }
  46.  
  47. public interface INonCompoundedInterestRate
  48. {
  49. INonCompoundedInterestRate ImpliedRate(double factor,
  50. double time,
  51. DayCounter dayCounter);
  52. }
  53.  
  54. public class MyNonCompoundedInterestRate: INonCompoundedInterestRate
  55. {
  56. public INonCompoundedInterestRate ImpliedRate(double factor,
  57. double time,
  58. DayCounter dayCounter) { /* do smth*/ }
  59.  
  60. static MyNonCompoundedInterestRate()
  61. {
  62. InstanceMap.AddInstance(new MyNonCompoundedInterestRate());
  63. }
  64. }
  65.  
  66. public abstract class InterestRate {
  67. public static T ImpliedRate<T>(
  68. double factor,
  69. double time,
  70. DayCounter dayCounter
  71. ) where T : INonCompoundedInterestRate
  72. {
  73. return InstanceMap.GetInstance<T>().
  74. ImpliedRate(factor, time, dayCounter);
  75. }
  76. // ...
  77. }
  78.  
  79. public static class InterestRateFactories
  80. {
  81. static InterestRateFactories()
  82. {
  83. _factories = new List<IInterestRateFactory>();
  84. // register default factories, although you can also register them elsewhere, like in your ioc setup
  85. }
  86. private static readonly List<IInterestRateFactory> _factories;
  87. public static void RegisterFactory(IInterestRateFactory factory)
  88. {
  89. _factories.Add(factory);
  90. }
  91. public static T ImpliedRate<T>(double factor, double time, DayCounter dayCounter)
  92. where T : NonCompoundedInterestRate
  93. {
  94. var factory = _factories.FirstOrDefault(x => x.CanCreate(typeof(T), false));
  95. if (factory == null)
  96. {
  97. throw new NotSupportedException("Cannot create a non-compounded implied interest rate of type " + typeof(T).Name);
  98. }
  99. return (T)factory.Create(factor, time, dayCounter);
  100. }
  101. public static T ImpliedRate<T>(double factor, double time, DayCounter dayCounter, Frequency frequency)
  102. where T : CompoundedInterestRate
  103. {
  104. var factory = _factories.FirstOrDefault(x => x.CanCreate(typeof(T), false));
  105. if (factory == null)
  106. {
  107. throw new NotSupportedException("Cannot create a compounded implied interest rate of type " + typeof(T).Name);
  108. }
  109. return (T)factory.Create(factor, time, dayCounter, frequency);
  110. }
  111. }
  112.  
  113. public interface IInterestRateFactory
  114. {
  115. bool CanCreate(Type nonCompoundedInterestRateType, bool compounded);
  116. NonCompoundedInterestRate Create(double factor, double time, DayCounter dayCounter);
  117. CompoundInterestRate Create(double factor, double time, DayCounter dayCounter, Frequency frequency);
  118. }
Add Comment
Please, Sign In to add comment