Set and Get difference in Object-Oriented Programming [duplicate]

this question already has answers here : how, when and where to use PHP magic methods? Get and Set? What's the difference? Should I use them in every class I create? (2 response) Closed for 3 years.

What is the basic difference of set and get in Object-Oriented Programming?

Author: Maniero, 2017-07-10

2 answers

Assuming you want to know about a property or prefix method set and get, this pattern is used to abstract a state that should be internal to the object. They are used as facilitators in cases of fields that must be accessed simply and directly for to take the value of an attribute (get) or to change the value of this attribute (set).

They are also called accessors and changers.

In general it is said that they cause a illusion of encapsulation, because encapsulating really is to create methods that do something special that does not only encapsulate access or assignment, it is to create a meaning for the method. It is an abstraction but not an encapsulation.

They are used to hide the detail of how the state is stored and / or calculated, validated, etc. Hiding the detail is an abstraction.

There is a current that says they should always be used, even if it does not do any additional processing other than taking the value (get) or assigning the value (ser) to the class variable. So if one day you need to put something just changes the implementation of this pair of methods and everyone who consumes the object passes use the new way. This is a indirection .

But there are those who are more pragmatic and only use when necessary, not least because in general it is easy to refactor.

There are still who it says that they are always an error because methods should have a function of their own, just letting pick up or change the data of a field is exposing detail in disguise.

Some languages have facilities to make this pair of methods look like a simple variable, is the case with C # . It can be seen how it is different in Java .

They can be used separately. In fact it is common to have a get and not a set. In some cases set becomes private or protected so that only the object itself can access it.

See arguments to use in Java .

There is a explanation when to use in PHP.

See how it is used in Python .

Example in pseudocode:

class Cliente { //exemplo bem meia boa
    private string nome; //note que é privado
    public string getNome() => return nome; //aqui pega a variável interna e retorna
    public void setNome(string nome) => this.nome = nome; //aqui joga o parâmetro na variável da classe
    private decimal saldo;
    public decimal getSaldo() => return saldo - 100; //dá uma margem de erro
    public void setSaldo(decimal valor) => if (valor > 200) saldo = valor; //só aceita um mínimo de 200
}

I put on GitHub for future reference.

 5
Author: Maniero, 2020-12-08 12:45:44

The method (GET) serves to retrieve a data and the method (SET) serves to modify a Data

In fact, there are many good reasons to consider using methods (getters and setters) instead of directly exposing fields of a class - in addition to the encapsulation argument and facilitating future changes.

Here are some of the reasons:

  • encapsulation of the behavior associated with obtaining or configuring property-this allows the additional functionality (such as validation) be added more easily later.
  • hiding the internal representation of the property when exposing a property using an alternative representation.
  • isolating your public interface from change-allowing the public interface remains constant while deployment changes without affecting existing consumers.
  • controlling life and memory management semantics (disposal) of property - particularly important in environments unmanaged memory (such as C ++ or Objective-C).
  • provide a debug intercept point for when a property changes at runtime - debugging when and where a property changed to a specific value can be quite difficult without this in some languages.
  • allow heirs to change the semantics of how the property behaves and is exposed by overriding the getter methods / setter.
  • Getters and setters can allow different levels of access - for example, the get can be public, but the set can be protected.
 3
Author: Amadeu Antunes, 2017-07-11 03:03:07