A Mediator Class ( Interposer Class
) is a class that, hierarchically speaking, positions itself between a classe ancestral
( from which it derives ) and an object of the type of this class . It has the main characteristic of having the same name of its ancestor class , which makes its application simplified in systems that already have declared objects of this class type.
In order for a CM ( Mediator Class ) to work, it is imperative that the CM declaration is between the declaration of the original class class > and the object declaration of the CM type. The interposer Class can even be in a unit
, as long as this unit
is positioned AFTER the unit
that contains the ancestral class in the uses
and provided that the uses
clause in question is BEFORE the declaration of CM-type objects.
Implementing the Mediator Class
Now let's increase the mediator class so that it does what we want. The implementation does not differ from what would be done if we had made a component correctly, for example. The cool thing about CM is that it has nothing special about it, other than its specific position within the hierarchy. Let's first get our TEdit to accept numbers only. This is well manjado. Here 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 in this Site :
In Delphi, Class Helper is a feature that allows add methods and modify component behavior at runtime, without the need to inherit the class or use compositing features >, making it appear that the class contains the new behavior since its inception.
Another difference is that Class Helpers does not only apply to components. They also allow you to modify domain classes created by the developer, add methods to record structures, and primitive types (introduced from the XE3 version). In the latter case, however, you can not add properties.
In the code below is a unit
that implements a Class Helper
to add to the TEdit
component the ability to validate your text, 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.
Line 9: syntax for Class Helper declaration. This instruction
indicates that a Class Helper
(which is a class) is being created for
modify the component TEdit
;
Lines 10 through 15: declaring class methods;
Lines 24 to 37: implementation of class methods.
Notice that the entire structure of Class Helper
is the same as a conventional class, changing only the form of declaration.
How to use
Whenever it is necessary to use a component that has the new behavior, simply add the name of the unit where the Class Helper
is declared in the uses
section. From there, you can access the new methods added, as shown in Listing 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 OnExit
event of the TEdit
component, and on lines 3 and 7 we use the new methods added by Class Helper
.
Modifying existing methods
In addition to adding new methods, Class Helper
allows us to modify existing methods in a component. In the code below we present an example of modifying the Clear
method to a special situation in a form where the TEdit
components that accept text receive the string "xxxx" when they are cleaned, and those that only accept numbers receive the text "0000 ". This behavior may be necessary on some system screens, from which we have, for example, to print the content on dot-matrix printers. In these cases, in continuous form sheets, fields can not be blank to avoid mischaracterising 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 this unit
in the uses
section and call the Clear
method of a TEdit
, this new method will be executed, instead of the default procedure that deletes all field text .
Retrieved from the Site devmedia.com.br >