Dependencies.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. unit Dependencies;
  2. interface
  3. uses
  4. System.SysUtils,
  5. Quick.Commons,
  6. Quick.IOC;
  7. type
  8. IMathService = interface
  9. ['{E8BEE282-3828-4F27-869D-C808296F8CA2}']
  10. procedure IncNumsOperations;
  11. function NumOperations : Integer;
  12. end;
  13. ISumService = interface(IMathService)
  14. ['{7580F5F6-0132-49F3-A22A-A2E93E53E8A7}']
  15. function Sum(a, b : Integer) : Integer;
  16. end;
  17. IMultService = interface(IMathService)
  18. ['{56772C4B-70AF-4BB7-9DFE-E2D67912DDC1}']
  19. function Mult(a, b : Integer) : Integer;
  20. end;
  21. TMathService = class(TInterfacedObject,IMathService)
  22. private
  23. fNumOperations : Integer;
  24. public
  25. constructor Create;
  26. procedure IncNumsOperations;
  27. function NumOperations : Integer;
  28. end;
  29. TSumService = class(TMathService,ISumService)
  30. public
  31. function Sum(a, b : Integer) : Integer;
  32. end;
  33. TMultService = class(TMathService,IMultService)
  34. public
  35. function Mult(a, b : Integer) : Integer;
  36. end;
  37. IBigService = interface
  38. ['{AE7E7617-02BD-48C9-A370-49566A563C38}']
  39. function GetNumOperations : Integer;
  40. function Sum(a, b : Integer) : Integer;
  41. function Mult(a, b : Integer) : Integer;
  42. function SumService : ISumService;
  43. function MultService : IMultService;
  44. property NumOperations : Integer read GetNumOperations;
  45. end;
  46. TBigService = class(TInterfacedObject,IBigService)
  47. private
  48. fSumService : ISumService;
  49. fMultService : IMultService;
  50. fNumOperations : Integer;
  51. function GetNumOperations : Integer;
  52. procedure IncNumOperations;
  53. public
  54. constructor Create(aSumService : ISumService; aMultService : IMultService);
  55. function Sum(a, b : Integer) : Integer;
  56. function Mult(a, b : Integer) : Integer;
  57. function SumService : ISumService;
  58. function MultService : IMultService;
  59. property NumOperations : Integer read GetNumOperations;
  60. end;
  61. TDivideService = class
  62. private
  63. fNumOperations : Integer;
  64. fRound : Boolean;
  65. public
  66. constructor Create(aRound : Boolean);
  67. property NumOperations : Integer read fNumOperations;
  68. function Divide(a,b : Integer) : Integer;
  69. end;
  70. implementation
  71. { TSumService }
  72. function TSumService.Sum(a, b: Integer): Integer;
  73. begin
  74. Result := a + b;
  75. IncNumsOperations;
  76. end;
  77. { TMultService }
  78. function TMultService.Mult(a, b: Integer): Integer;
  79. begin
  80. Result := a * b;
  81. IncNumsOperations;
  82. end;
  83. { TBigService }
  84. constructor TBigService.Create(aSumService: ISumService; aMultService: IMultService);
  85. begin
  86. fSumService := aSumService;
  87. fMultService := aMultService;
  88. end;
  89. function TBigService.Sum(a, b: Integer): Integer;
  90. begin
  91. Result := fSumService.Sum(a,b);
  92. IncNumOperations;
  93. end;
  94. function TBigService.SumService: ISumService;
  95. begin
  96. Result := fSumService;
  97. end;
  98. function TBigService.GetNumOperations: Integer;
  99. begin
  100. Result := fNumOperations;
  101. end;
  102. procedure TBigService.IncNumOperations;
  103. begin
  104. Inc(fNumOperations);
  105. end;
  106. function TBigService.Mult(a, b: Integer): Integer;
  107. begin
  108. Result := fMultService.Mult(a,b);
  109. IncNumOperations;
  110. end;
  111. function TBigService.MultService: IMultService;
  112. begin
  113. Result := fMultService;
  114. end;
  115. { TMathService }
  116. constructor TMathService.Create;
  117. begin
  118. fNumOperations := 0;
  119. end;
  120. procedure TMathService.IncNumsOperations;
  121. begin
  122. Inc(fNumOperations);
  123. end;
  124. function TMathService.NumOperations: Integer;
  125. begin
  126. Result := fNumOperations;
  127. end;
  128. { TDivideService }
  129. constructor TDivideService.Create(aRound : Boolean);
  130. begin
  131. fNumOperations := 0;
  132. fRound := aRound;
  133. end;
  134. function TDivideService.Divide(a, b: Integer): Integer;
  135. begin
  136. if fRound then Result := Round(a / b)
  137. else Result := Trunc(a / b);
  138. Inc(fNumOperations);
  139. end;
  140. end.