Difference between Getter \ Setter and Property in Delphi

8

There are a few ways to encapsulate the attributes, of which I find the most interesting is the one that uses the Getter and Setters methods to access the attributes (Similar to Java.) I've been reading and downloading material on the subject, and noticed that most programmers use properties . I can not understand the "advantage" of using property, because in the end they are also "accessing" attribute values through Getter and Setters. I do not know if I could express myself clearly, but in short, I would like to know the difference or disadvantages between them.

    
asked by anonymous 07.06.2015 / 18:38

3 answers

3

Based on experience only, I say that using methods, a method to get the value, and another to set the value, is a waste of unnecessary code unless you have rules (validations / checks) to be made on the value that will be obtained or set to field .

In Delphi , the default code used is, for example:

TPessoa = class
private
  FNome: string;
public
  property Nome: string read FNome write FNome;
end;
// e usa-se assim
pessoa := TPessoa.Create;
pessoa.Nome := 'Fulano';

Where there is a Nome property that directly accesses field FNome (in Delphi ) field and not attribute ). The same could be written as follows:

TPessoa = class
private
  FNome: string;
  private SetNome(AValue: string);
  function GetNome: string;
public
  property Nome: string read GetNome write SetNome;
end;
...
private TPessoa.SetNome(AValue: string);
begin
  // possíveis verificações e validações ...
  FNome := AValue;
end;

procedure TPessoa.GetNome: string;
begin
  // possíveis verificações e validações ...
  result := FNome;
end;

Here it is used in the same way:

pessoa := TPessoa.Create;
pessoa.Nome := 'Fulano';    

In this way, you use the GetNome method when trying to retrieve the property value and SetNome to set the value.

This is where you probably wondered about the need to use property with Get and Set methods and you probably wondered why not just use the SetXXX and GetXXX methods methods, as in the example?

TPessoa = class
private
  FNome: string;
public
  private SetNome(AValue: string);
  function GetNome: string;
end;

The default with property is a custom that comes from time.
For C # , over time it was allowed to write properties with anonymous methods:

public class Pessoa
{
    public string Nome { get; set; }
}

// e você usa assim:
var pessoa = new Pessoa();
pessoa.Nome = 'Fulano';

And the main issue of your taste is most likely your custom with Java .

If I'm not mistaken, using Java 8 Well, especially if I'm not mistaken, it leads me to the conclusion that this method has been considered more and more practical than accessing values from fields only by in Get and Set .

Finally, as I said that I was only relying on my own experience, what coding standard to use is not an obligation but rather an optional one, so it's up to you whether the Delphi application will only be serviced by you or if it goes for others with experience only in Delphi. If you still want to just use the Getters and Setters methods

The difference in use between one form and another I believe is just the extra effort for the class user to access a field methods rather than directly by the property.

About the advantages and disadvantages: I believe there are only advantages to using the coding standards of each language, because it makes it more intelligible by another programmer who comes by hand in its source code.

I believe that everyone who programs in one language and then needs to program in another tends to want to use the same coding pattern in both.

    
07.06.2015 / 20:55
2

Your difficulty is precisely because you are not used to seeing the code like that. Apparently Java did not have properties before and that's why it uses getters / setters.

Some benefits of using properties:

  • The code becomes much clearer when using properties ( property ).
  • You can easily change a "read-only" property to "Read and Write", and vice versa, without having to search the code for getters and setters.
  • Using INDEX you can pass an index when accessing the properties of the class;

    TMyRect = class
    private
      FValues: Array[0..3] of Integer;
      function GetTop(Index: Integer): Integer;
      function GetLeft(Index: Integer): Integer
      function GetWidth(Index: Integer): Integer
      function GetHeight(Index: Integer): Integer
    public
      property Top    Index 0 read GetTop;
      property Left   Index 1 read GetLeft;
      property Width  Index 2 read GetWidth;
      property Height Index 3 read GetHeight;
    end;
    
    
    function TMyRect.GetTop(Index: Integer): Integer;
    begin
      Result := FValues[Index];
    end; 
    
  • Properties can be accessed by the Object Inspector;

  • Two SOen questions on this subject helped me to learn more about:

    link

    link

        
    12.06.2015 / 19:41
    0

    I think the main advantage of using property rather than getter and setter in Delphi is both read and write access to a single attribute.

    With getters and setters we would have: getNome and setName. (Example: person.setName ('So-and-so'))

    With property we would have: Name only. (Example: person.name: = 'Other').

    In this link there is more useful information.

        
    08.06.2015 / 21:15