How to implement the Builder pattern in Delphi using records without creating copies?

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:

  1. 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.

  2. 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.

  3. 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?

0

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;

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật