Using Interfaces with Procedures

From RAD Studio
Jump to: navigation, search

Go Up to Using Interfaces


Interfaces allow you to write generic procedures that can handle objects without requiring that the objects descend from a particular base class. Using the IPaint and IRotate interfaces defined previously, you can write the following procedures:

procedure PaintObjects(Painters: array of IPaint);
var
  I: Integer;
begin
  for I := Low(Painters) to High(Painters) do
    Painters[I].Paint;
end;
procedure RotateObjects(Degrees: Integer; Rotaters: array of IRotate);
var
  I: Integer;
begin
  for I := Low(Rotaters) to High(Rotaters) do
    Rotaters[I].Rotate(Degrees);
end;

RotateObjects does not require that the objects know how to paint themselves and PaintObjects does not require the objects know how to rotate. This allows the generic procedures to be used more often than if they were written to only work against a TFigure class.

See Also