Calculating the average value

As an example, the TemplateMethod project from the Template method folder implements two specialized versions of a shared template method. The complete definition of the base object and template method is shown here:

type  TDataAggregatorTemplate = class  protected    procedure CloseDataSource; virtual; abstract;    function GetNextValue(var value: integer): boolean; virtual; abstract;    function OpenDataSource: boolean; virtual; abstract;  public    function CalculateAverage: real;  end;function TDataAggregatorTemplate.CalculateAverage: real;var  sum: integer;  total: integer;  value: integer;begin  if not OpenDataSource then    raise Exception.Create('Failed!');  try    total := 0;    sum := 0;    while GetNextValue(value) do    begin Inc(total); ...

Get Hands-On Design Patterns with Delphi now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.