Jump to content

Ethan Yager

Members
  • Content Count

    1
  • Joined

  • Last visited

Posts posted by Ethan Yager


  1. I can't seem to implement the visitor pattern in Delphi without circular references. Does anyone know of a way to implement the visitor pattern without having all of the classes in one big unit? For instance, if I have a complex tree structure (psuedo code so that I can demonstrate concisely):

     

    Unit Base

    type

    TBase = class

    end;

    -------------------------------------------------------

    Unit Bolt

    uses Base;

    TBolt = class(TBase)

      procedure Tighten;

      procedure Untighten;

     

      Visit(visitor: TAutomobileVisitor); <-- Requires circular reference to Automobile unit 

    end;

    -------------------------------------------------------

    Unit Door

    uses Base, Bolt;

    type

    TDoor = class(TBase)

      Bolts: array of TBolt;

      procedure Open;

      procedure Close;

     

      Visit(visitor: TAutomobileVisitor);  <-- Requires circular reference to Automobile unit

    end;

    -------------------------------------------------------

    Unit Seat

    uses Base, Bolt;

    type

    TSeat = class(TBase)

      Bolts: array of TBolt;

      procedure Heat(on: boolean);

     

      Visit(visitor: TAutomobileVisitor);  <-- Requires circular reference to Automobile unit

    end;

    -------------------------------------------------------

    Unit Automobile

    uses Base, Door, Seat, Bolt;

    type

    TAutomobile = class(TBase)

      FOwnerName: string;

      Doors: array of TDoor;

      Seats: array of TSeat;

     

      Visit(visitor: TAutomobileVisitor); 

    end;

     

    TAutomobileVisitor = class

      procedure Visit(o: TAutomobile); overload; virtual;

      procedure Visit(o: TBolt); overload; virtual;

      procedure Visit(o: TDoor); overload; virtual;

      procedure Visit(o: TSeat); overload; virtual;

    end;

    -------------------------------------------------------

     

    I have tried moving things around and using interfaces but can't seem to avoid circular references.

     

×