I am trying to implement the Builder design pattern in Delphi using records. My goal is to allow chaining of methods, similar to how it’s done in Kotlin, Java, or Swift. For example:
val alertDialog = AlertDialog.Builder(this)
.setTitle("Builder Example")
.setMessage("This is a builder pattern example")
.create()
In Delphi, I wrote the following code using a record:
type
TAlertDialogBuilder = record
private
FTitle: string;
FMessage: string;
public
function SetTitle(const ATitle: string): TAlertDialogBuilder;
function SetMessage(const AMessage: string): TAlertDialogBuilder;
procedure CreateAndShow;
end;
implementation
function TAlertDialogBuilder.SetTitle(const ATitle: string): TAlertDialogBuilder;
begin
FTitle := ATitle;
Result := Self; // Creates a copy
end;
function TAlertDialogBuilder.SetMessage(const AMessage: string): TAlertDialogBuilder;
begin
FMessage := AMessage;
Result := Self; // Creates a copy
end;
procedure TAlertDialogBuilder.CreateAndShow;
begin
ShowMessage(Format('Title: %s%sMessage: %s', [FTitle, sLineBreak, FMessage]));
end;
I use it like this:
procedure ShowDialogExample;
begin
TAlertDialogBuilder.Create
.SetTitle('Builder Example')
.SetMessage('This is a builder pattern example')
.CreateAndShow;
end;
The Problem:
Since records are value types in Delphi, returning Self
in methods like SetTitle
and SetMessage
results in a copy of the record. This is not efficient, especially if the record has many fields or large data.
What I’ve Tried:
Returning a Pointer to the Record: I modified the methods to return a pointer to the record (PAlertDialogBuilder
) instead of Self
. While this avoids copies, it makes chaining less intuitive because I need to dereference the pointer with ^
for subsequent calls.
Using absolute
: I considered using the absolute
keyword to avoid copies, but I couldn’t figure out a way to make it work for chaining method calls.
Switching to Classes: I know classes avoid this issue because they are reference types, but I’d prefer to stick with records if possible for their lightweight nature and value semantics.
My Question:
How can I implement the Builder pattern in Delphi using records while avoiding unnecessary copies? Is there a way to use absolute
or another approach to achieve this efficiently?
I need to dereference the pointer with ^ for subsequent calls.
No, you don't. Dereferencing a pointer with ^
is optional. This is documented:
Using Extended Syntax with Pointers
The
{$EXTENDED}
compiler directive affects the use of the caret (^). When{$X+}
is in effect (the default), you can omit the caret when referencing pointers. The caret is still required when declaring a pointer and for resolving the ambiguity when a pointer points to another pointer. For more information, see Extended syntax (Delphi).With extended syntax enabled, you can omit the caret when referring to a pointer, as in the following example:
{$X+} type PMyRec = ^TMyRec; TMyRec = record Data: Integer; end; var MyRec: PMyRec; begin New(MyRec); MyRec.Data := 42; {#1} end.
When extended syntax is not enabled, the line marked {#1} would typically be expressed as:
MyRec^.Data := 42;
Is there... another approach to achieve this efficiently?
One option would be to change the Builder itself to be an interfaced class. That way, you pass around references, and you avoid copies and leaks.
For example:
type
IAlertDialogBuilderIntf = interface
function SetTitle(const ATitle: string): IAlertDialogBuilderIntf;
function SetMessage(const AMessage: string): IAlertDialogBuilderIntf;
procedure CreateAndShow;
end;
TAlertDialogBuilderImpl = class(TInterfacedObject, IAlertDialogBuilderIntf)
private
FTitle: string;
FMessage: string;
public
function SetTitle(const ATitle: string): IAlertDialogBuilderIntf;
function SetMessage(const AMessage: string): IAlertDialogBuilderIntf;
procedure CreateAndShow;
end;
TAlertDialog = class
...
function Builder(params): IAlertDialogBuilderIntf;
...
end;
implementation
function TAlertDialogBuilderImpl.SetTitle(const ATitle: string): IAlertDialogBuilderIntf;
begin
FTitle := ATitle;
Result := Self;
end;
function TAlertDialogBuilderImpl.SetMessage(const AMessage: string): IAlertDialogBuilderIntf;
begin
FMessage := AMessage;
Result := Self;
end;
procedure TAlertDialogBuilderImpl.CreateAndShow;
begin
ShowMessage(Format('Title: %s%sMessage: %s', [FTitle, sLineBreak, FMessage]));
end;
function TAlertDialog.Builder(params): IAlertDialogBuilderIntf;
begin
Result := TAlertDialogBuilderImp.Create as IAlertDialogBuilderIntf;
end;