RAD Studio |  |

Using Interfaces with Procedures
See Also
Collapse All

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:

Copy Code

    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

Using Interfaces Across the Hierarchy

Implementing IInterface

Using the As Operator with Interfaces

Reusing Code and Delegation

Memory Management of Interface Objects

Using Interfaces in Distributed Applications

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