Evgenii Legotckoi
Sept. 21, 2018, 1:13 p.m.

Example - "Builder" design pattern in Delhpi

Content

The functionality used in Delphi's VCL to create forms and components is similar in concept to the builder. Delphi creates forms using a common interface, through Application.CreateForm and through the TForm class constructor. TForm implements a common constructor using the resource information (DFM file) to instantiate the components owned by the form. Many descendant classes reuse this same construction process to create different representations. Delphi also makes developer extensions easy. TForm's OnCreate event also adds a hook into the builder process to make the functionality easy to extend.


Example

The following example includes a class TAbstractFormBuilder and two concrete classes  TRedFormBuilder and TBlueFormBuilder. For ease of development some common functionality of the concrete classes has been moved into the shared TAbstractFormBuilder class.

  1. type
  2. TAbstractFormBuilder = class
  3. private
  4. FForm: TForm;
  5. procedure BuilderFormClose(Sender: TObject; var Action: TCloseAction);
  6. protected
  7. function GetForm: TForm; virtual;
  8. public
  9. procedure CreateForm(AOwner: TComponent); virtual;
  10. procedure CreateSpeedButton; virtual; abstract;
  11. procedure CreateEdit; virtual; abstract;
  12. procedure CreateLabel; virtual; abstract;
  13. property Form: TForm read GetForm;
  14. end;
  15.  
  16. type
  17. TRedFormBuilder = class(TAbstractFormBuilder)
  18. private
  19. FNextLeft, FNextTop: Integer;
  20. public
  21. procedure CreateForm(AOwner: TComponent); override;
  22. procedure CreateSpeedButton; override;
  23. procedure CreateEdit; override;
  24. procedure CreateLabel; override;
  25. end;
  26.  
  27. type
  28. TBlueFormBuilder = class(TAbstractFormBuilder)
  29. private
  30. FNextLeft, FNextTop: Integer;
  31. public
  32. procedure CreateForm(AOwner: TComponent); override;
  33. procedure CreateSpeedButton; override;
  34. procedure CreateEdit; override;
  35. procedure CreateLabel; override;
  36. end;

At runtime the client application instructs one of the concrete classes to create parts using the public part creation procedures. The concrete builder instance is passed to the following procedure:

  1. procedure TForm1.Create3ComponentFormUsingBuilder(ABuilder: TAbstractFormBuilder);
  2. var
  3. NewForm: TForm;
  4. begin
  5. with ABuilder do begin
  6. CreateForm(Application);
  7. CreateEdit;
  8. CreateSpeedButton;
  9. CreateLabel;
  10. NewForm := Form;
  11. if NewForm <> nil then NewForm.Show;
  12. end;
  13. end;

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html