[fpc-pascal] Using interfaces for compile time testing

Graeme Geldenhuys graemeg.lists at gmail.com
Mon Jun 25 11:42:02 CEST 2007


Hi,

I'm pretty new to using interfaces... I want to use interfaces so that
I can spot problems at compile time and not runtime.  For example: If
I used a base class with abstract methods and then created descendant
classes and forget to implement one of the abstract methods which gets
called somewhere...  I'll only find the problem at runtime and not
compile time.

I'm hoping that Interfaces will solve this issue.  Am I allowed to do
the following?
This seems to work. As soon as I leave out one of the IWindowImpl
interface methods from the TX11Window class, the compiler complains.
Also can I let my base class descend from TComponent instead of
TInterfacedObject? Looking at the declaration of the TComponent class,
it implements IUnknown, so I guess I am allowed to add more interfaces
in descendant classes.

Is there any things I need to watch out for?

type
  IWindowImpl = interface;

  IWindowImpl = interface(IInterface)
    ['{BDBC9BE7-77E0-4EA1-B40B-282CE06A32B3}']
    procedure DoAllocateWindowHandle(AParent: IWindowImpl);
    procedure DoSetWindowTitle(const ATitle: string);
  end;

  TBaseImpl = class(TComponent)
  private
    FHeight: integer;
    FWidth: integer;
    procedure SetHeight(const AValue: integer);
    procedure SetWidth(const AValue: integer);
  public
    property  Width: integer read FWidth write SetWidth;
    property  Height: integer read FHeight write SetHeight;
  end;

  TX11Window = class(TBaseImpl, IWindowImpl)
  protected
    procedure DoAllocateWindowHandle(AParent: IWindowImpl);
    procedure DoSetWindowTitle(const ATitle: string);
  end;


Regards,
  - Graeme -



More information about the fpc-pascal mailing list