Interceptor classes

WILD

Administrator
Staff member
ADMIN
SELLER
SUPREME
MEMBER
Joined
Jan 21, 2025
Messages
219
Reaction score
637
Deposit
0$
Sometimes it becomes necessary to change the behavior of a particular VCL control, or add functionality to it. Of course, you can create a new component that inherits from the necessary one, add it to the palette and use it. But this is not always justified. Sometimes you need to change the functionality quite a bit, and not in all projects, or even on all project forms, but only in one place. In such cases, interceptor classes come to the rescue.

The idea is very simple: we inherit from the desired class, add functionality or redefine methods, changing the existing functionality, and then pass our modified class to the compiler instead of the standard one. To achieve this, the substitution must be performed before the form class is described.


Let's set a task: when any button on the form is clicked, perform an additional action in addition to the action specified by the OnClick event handler. In Delphi, this can be achieved as follows:
type
TButton = class({Vcl.}StdCtrls.TButton) // Vcl. добавляется в новых версиях Дельфи
public
procedure Click; override; // Для примера - изменяем функционал метода Click
end;

TForm1 = class(TForm)
// ...
Button1: TButton; // У всех кнопок, лежащих на форме уже будет изменен функционал Click
// ...
end; { TForm1 }

implementation

{ TButton }

procedure TButton.Click;
begin
inherited;
// дополнительные действия
end;

// ...

 
Top Bottom