Home > Delphi > Fun with nested classes

Fun with nested classes

A bit overkill, but fun none the less. This post is in reply to georgba on facebook. Here is an example of how to use nested classes. When compiled and you ship the DCU, it will be harder to resource your code.

  TBaseDoc = Class(TObject)
  protected
    type
    TLoaderClass = Class(TObject)
    public
      function getFormatValue(aValue:Integer):String;virtual;
    End;
  strict protected
    function    getAdapter:TLoaderClass;virtual;
  strict private
    FAdapter:   TLoaderClass;
  public
    Property    Adapter:TLoaderClass read FAdapter;
    Constructor Create;virtual;
    Destructor  Destroy;Override;
  End;

  TTextDoc = Class sealed(TBaseDoc)
  protected
    type
    TLoaderClass = Class(TBaseDoc.TLoaderClass)
    public
      function getFormatValue(aValue:Integer):String;override;
    End;
  strict protected
    function getAdapter:TBaseDoc.TLoaderClass;override;
  end;

And ofcourse:

{ TBaseDoc }

constructor TBaseDoc.Create;
begin
  inherited Create;
  FAdapter:=getAdapter;
end;

destructor TBaseDoc.Destroy;
begin
  FAdapter.Free;
  inherited;
end;

Function TBaseDoc.TLoaderClass.getFormatValue(aValue: Integer):String;
begin
  raise exception.Create('NOT IMPLEMENTED');
end;

function TBaseDoc.getAdapter:TLoaderClass;
begin
  result:=TLoaderClass.Create;
end;

{ TTextDoc }

function TTextDoc.getAdapter:TBaseDoc.TLoaderClass;
begin
  result:=TLoaderClass.Create;
end;

Function TTextDoc.TLoaderClass.getFormatValue(aValue: Integer):String;
begin
  result:=IntToStr(aValue);
end;

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: