Evgenii Legotckoi
Sept. 18, 2018, 3:11 p.m.

Example - Abstract Factory in Delphi

Content

This pattern is ideal where you want to isolate your application from the implementation of the concrete classes. For example if you wanted to overlay Delphi's VCL with a common VCL layer for both 16 and 32 bit applications, you might start with the abstract factory as a base.


Example

The following example uses an abstract factory and two concrete factory classes to implement different styles of user interface components. TOAbstractFactory is a singleton class, since we usually want one factory to be used for the whole application.

  1. TOAbstractFactory = class(TObject)
  2. public
  3. constructor Create;
  4. destructor Destroy; override;
  5.  
  6. //abstract widget constructors
  7. function CreateSpeedButton(AOwner: TComponent): TSpeedButton; virtual; abstract;
  8. function CreateEdit(AOwner: TComponent): TEdit; virtual; abstract;
  9. function CreateLabel(AOwner: TComponent): TLabel; virtual; abstract;
  10. end;

TORedFactory and TOBlueFactory override the abstract interface to support different widget styles.

  1. TORedFactory = class(TOAbstractFactory)
  2. public
  3. //concrete widget constructors
  4. function CreateSpeedButton(AOwner: TComponent): TSpeedButton; override;
  5. function CreateEdit(AOwner: TComponent): TEdit; override;
  6. function CreateLabel(AOwner: TComponent): TLabel; override;
  7. end;
  8.  
  9. TOBlueFactory = class(TOAbstractFactory)
  10. public
  11. //concrete widget constructors
  12. function CreateSpeedButton(AOwner: TComponent): TSpeedButton; override;
  13. function CreateEdit(AOwner: TComponent): TEdit; override;
  14. function CreateLabel(AOwner: TComponent): TLabel; override;
  15. end;

At runtime, our client application instantiates the abstract factory with a concrete class and then uses the abstract interface. Parts of the client application that use the factory don't need to know which concrete class is actually in use.

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