Legacy interface in Delphi

Asked

Viewed 1,324 times

1

I need to make a multiple inheritance in Delphi. Reading some articles, the most appropriate way is to make 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 am currently creating two variables and instantiating one, according to the type of user selection. I wanted to encompass two in one and to 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;
  • Where is the multiple inheritance you want? If possible, create a simple UML diagram to show the inheritances.

  • It would be for the classes of PF and PJ, because in both already have the command to record person. I just wanted to instantiate an object that I can choose between PF and PJ. The way I’m doing, I need to create two variables

1 answer

3


If you want to share the write method 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 by declaring this method in the base class Tpessoa and just call it-no need to declare 2 methods one for each class.

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

Use :

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 engraving method is different for each legal and physical class, its problem can be solved using polymorphism which is one of the characteristics of the POO. Include an abstract method in the base class "Tpessoa" with the virtual directive and Abstract.

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 note that abstract methods should not be written in the class where they have been declared, then there will be no routine body for the class save method TPessoa, there will be only your statement.

Write down the recording methods of each daughter class. With this strategy you have a record method for each instantiated class and with only one call method and with the advantage of having your record method specific to each of the classes.

Another technique for solving this problem is applying the design standard Strategy, which also uses polymorphism but otherwise, but I believe that the solution I mentioned should solve.

  • Thank you. I’m going to study this concept. Yes, the recording methods are specific to each class. Only the people record method has features for both.

Browser other questions tagged

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