Interface with inheritance in Delphi

1

I need to make a multiple inheritance in Delphi. Reading some articles, the most appropriate way is to do an interface.

How do I do this, knowing that we have the classes below as an example:

type
TPessoa = class
//atributos e métodos
end;

type
TPessoaFisica = class(TPessoa)
//atributos e métodos
end;

type
TPessoaJuridica = class(TPessoa)
//atributos e métodos
end;

I'm currently creating two variables and instantiating one, depending on the type of user selection. I wanted to span two in one and instantiate as needed.

var
  CadPJ: TCadastroPJ;
  CadPF: TCadastroPF;
begin

  if rdTipoPessoa.ItemIndex = 0 then
  begin

    try

      CadPF:= TCadastroPF.Create;
      CadPF.LicencaIDCliente:= idCliente;
      CadPF.NomePessoa:= edtNome.Text;
      CadPF.gravarPF;

    finally
      CadPF.Free;
    end;

  end
  else
  begin

    try

      CadPJ:= TCadastroPJ.Create;
      CadPJ.LicencaIDCliente:= idCliente;
      CadPJ.NomeFantasia:= edtNomeFantasia.Text;
      CadPJ.gravarPJ;

    finally
      CadPJ.Free;
    end;
  end;
end;
    
asked by anonymous 23.05.2015 / 08:13

1 answer

3

If you want to share the method write to the 2 Physical and Legal classes because the write method will have the same behavior and same code for the 2 daughter classes, then make use of inheritance, declaring this method in the base class it does not need to declare 2 methods one for each class.

type
TPessoa = class
  //atributos e métodos
Public
  procedure gravar; 
end;

Usage:

var
  CadPF: TCadastroPF;
  CadPJ: TCadastroPJ;
begin
    CadPF:= TCadastroPF.Create;
    CadPJ:= TCadastroPJ.Create
    try
      CadPF.Gravar;
      CadPJ.Gravar;
    Finally
      CadPF.Fre;
      CadPJ.Free;
    End; 

However, if the write method is different for each physical and legal class, its problem can be solved by using polymorphism that is one of the characteristics of OOP. Include an abstract method in the "TPessoa" base class with the virtual and abstract directive.

Example:

type
TPessoa = class
  //atributos e métodos
Public
  procedure gravar; virtual; abstract;
end;

E sobrescreva o método gravar nas 2 classes filhas TPessoaFisica  e TPessoaJuridica  assim :

type
TPessoaFisica = class(TPessoa)
//atributos e métodos
public
   procedure gravar; override;
end;

type
TPessoaJuridica = class(TPessoa)
//atributos e métodos
public
   procedure gravar; override;
end;

In this case realize that abstract methods should not be written to the class where they have been declared, then there will not be a routine body for the write method of class TPessoa , there will be only your declaration.

Write the write methods of each child class. With this strategy you have a write method for each instanced class and with only one method of calling and with the advantage of having your method write specific to each of the classes.

Another technique for solving this problem is applying the Strategy project pattern, which also uses polymorphism but otherwise, but I think the solution I mentioned should resolve.     

26.05.2015 / 18:08