Just like Ozan said: most of the time, the good answer is to create a base class with virtual methods:
unit BaseWorld; TBaseWorld = class function GetWorldInfo() : TWorldInfo; virtual; {abstract;} ... unit Player; TPlayer = class FWorld : TBaseWorld; constructor Create( AWorld : TBaseWorld ); ... unit RealWorld; TWorld = class(TBaseWorld) function GetWorldInfo() : TWorldInfo; override; ... TWorld.AddPlayer(); begin TPlayer.Create(Self); end; ...
or, with a similar effect, publish the interface:
unit WorldIntf; IWorldInterface = interface function GetWorldInfo() : TWorldInfo; ... unit Player; TPlayer = class FWorld : IWorldInterface; constructor Create( AWorld : IWorldInterface ); ... unit RealWorld; TWorld = class(TInterfacedObject, IWorldInterface) function GetWorldInfo() : TWorldInfo; ... TWorld.AddPlayer(); begin TPlayer.Create(Self); end; ...
Depending on how your code works, you can hide the world behind an abstract layer (as in the examples above) or Player (as suggested by Ozan).
source share