What is the difference between a Mediator Class (Interposer Class) and a Helper Class (Helper Class) in Delphi?

Asked

Viewed 558 times

5

I’ve always worked more with interposer classes. I know it’s best to create components, but sometimes it’s faster to make a point adjustment using an interposer. New versions of Delphi have the concept of helper classes and I found similar to interposers, even seem simpler to implement, but is there any advantage in using interposers classes instead of helper classes?

  • 2

    Nice question, I can’t tell you the difference, but I usually use Interposer for more complex or visual classes, for example, Create a new rule on TfrxReport with buttons on the preview screen. Already the Class/Record Helper, for simpler methods Ex: Create a SaveToFile for a String. I don’t know if it’s right, but I’m looking forward to seeing the answers.

  • Yeah, I’d also like to see some response on that. Let’s wait :)

1 answer

2


  • Class Mediator

One Class Mediator (Interposer Class) is a class that, hierarchically speaking, stands between a classe ancestral (from which it derives) and the declaration of a object of the type of this class. It has as main characteristic the fact of having the same name of its ancestral class, what makes its application simplified in systems that already have objects declared of the type of this class.

So that an CM (Class Mediator) it is essential that the declaration of the original class (ancestral class) and the declaration of the CM type object. A interposer Class may even be in a unit separate, provided that this unit is positioned AFTER the unit containing the ancestral class in the clause uses and provided that the clause uses in question is BEFORE the declaration of the CM type objects.

Implementing the Mediator Class

We will now increase the mediating class to do what we want. The implementation does not differ from what would have been done if we had made a component correctly, for example. The cool thing about CM is that it has nothing special, except its specific positioning within the hierarchy. First let’s make our Tedit accept only numbers. This is very manly. Below is the code:

01  unit UFormPrincipal;
02
03  interface
04
05  uses
06    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
07    System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
08    Vcl.StdCtrls;
09   
10  type
11     TEdit = class(Vcl.StdCtrls.TEdit)
12     protected
13       procedure KeyPress(var PKey: Char); override;
14       procedure Loaded; override;
15     public
16     end;
17
18  TFormPrincipal = class(TForm)
19    EDIT: TEdit;
20    private
21    { Private declarations }
22    public
23    { Public declarations }
24    end;
25
26  var
27  FormPrincipal: TFormPrincipal;
28
29  implementation
30 
31  {$R *.dfm}
32
33  { TEdit }
34
35  procedure TEdit.KeyPress(var PKey: Char);
36  begin
37  inherited;
38    if not (PKey in ['0'..'9',#8,'-']) then
39      PKey := #0;
40  end;
41
42  procedure TEdit.Loaded;
43  begin
44  inherited;
45    Text := '';
46  end;
47  
48  end.

Acehi on this Website:


  • Class Helper

At Delphi, Class Helper is a resource that allows add methods and modify component behaviors at runtime, without the need to inherit the class or use composition features, making it appear that the class contains the new behavior since its creation.

Another differential is that Class Helpers do not apply only to components. They also allow you to modify developer-created domain classes, add methods to record type structures, and primitive types (introduced from XE3). In the latter case, however, it is not possible to add properties.

In the code below is presented a unit which implements a Class Helper to add to component TEdit the ability to validate your text by defining whether it represents a CPF or CNPJ.

01  unit UnitEditDocumento;
02     
03  interface
04  
05  uses
06     vcl.stdCtrls, SysUtils;
07  
08  type
09     TEditDocumento = class helper for TEdit
10     private
11        function TextoSemSinais: string;
12     public
13        function isPessoaFisica: boolean;
14        function isPessoaJuridica: boolean;
15  end;
16  
17  implementation
18  
19  uses
20     UnitUtils;
21  
22  { TEditDocumento }
23  
24  function TEditDocumento.TextoSemSinais: string;
25  var
26    //Retorna o texto sem caracteres especiais
27  end;
28  
29  function TEditDocumento.isPessoaFisica: boolean;
30  begin
31     result := length(TextoSemSinais) = 11;
32  end;
33  
34  function TEditDocumento.isPessoaJuridica: boolean;
35  begin
36     result := length(TextoSemSinais) = 14;
37  end;
38  end.
  1. Line 9: syntax for Class Helper declaration. This statement indicates that a Class Helper (which is a class) for modify the component TEdit;
  2. Rows 10 to 15: declaration of class methods;
  3. Lines 24 to 37: implementation of class methods.

Realize that the whole structure of Class Helper is the same as a conventional class, changing only the form of declaration.

How to use Where it is necessary to use a component that has the new behaviour, simply add the name of the Unit where the Class Helper in the section uses. From this, you can access the new methods added, as shown in List 2.

01  procedure TForm5.Edit1Exit(Sender: TObject);
02  begin
03     if Edit1.isPessoaFisica then
04        Label1.Caption:='Documento Pessoa Física'
05     else
06     begin
07        if Edit1.isPessoaJuridica then
08           Label1.Caption:='Documento Pessoa Jurídica'
09        else
10        begin
11           Label2.Caption:='Documento Inválido';
12           exit;
13        end;
14     end;
15  end;

In the Code above using the methods added by Class Helper In this code, we are working with the event OnExit of the component TEdit, and in lines 3 and 7 we use the new methods added by Class Helper.

Modifying existing methods In addition to adding new methods, the Class Helper allows us to modify existing methods in a component. In the code below we present an example of modification of the method Clear for a particular situation on a form where the components TEdit that accept text receive the string "xxxx" when it is cleaned, and those that accept only numbers receive the text "0000". This behavior may be necessary in some screens of the system, from which we have, for example, to print the contents in matrix printers. In such cases, in continuous form worksheets, fields cannot appear blank to avoid defacing the print.

01  Unit UnitEditMatricial;
02  ...
03  type
04     TEditDocumento = class helper for TEdit
05     public
06        procedure Clear;
07     end;
08  
09  ...
10  
11  procedure TEditDocumento.Clear;
12  begin
13     if NumbersOnly then
14        Text:= StringOfChar('0',MaxLength)
15     else
16        Text:=StringOfChar('X',MaxLength);
17  end;

**Class Helper* to modify an existing method in TEdit*

Now, when we add the reference to that unit in the section uses and activate the method Clear of a TEdit, this new method will be executed, instead of the standard procedure that eliminates all text from the field.

Taken from the Site devmedia.com.br

  • Man, thanks even for the answer. Very thorough and detailed! It was worth much more, because you used part of what I wrote on my website to answer my own question and it makes me very happy, since I know you found my content interesting! I asked this question in October 2016 and published the article about interposers in November. The question came up because I always used interposers and never used helpers, by the way, the question came up just while I was writing the article! Thank you very much for your reply

  • 1

    thanks! I saw your question and decided to search to answer I spent almost two hours to understand how it works... but it was worth it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.