RAD Studio
ContentsIndex
PreviousUpNext
Using Interfaces with Procedures

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.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!