Real example of using encapsulation

2

I researched encapsulation, I even read some topics here, but I have not yet seen a real example of using it in a way that shows me its advantages, what issues it avoids.

I have an attribute of type public , but it is customary for programmers to do private and create their properties get and set . Well, but never show a real example, a system they have already done, the advantages of using it and the problems that have occurred and been solved.

I've seen examples on YouTube videos, but I do not see why they switched from public to private , they do not show which problems they would avoid. I've read a few articles, I've seen some videos, but I just wanted a real, objective example showing the variables for private because it avoids that kind of problem and the advantages are just that.

    
asked by anonymous 24.04.2018 / 22:42

2 answers

4
  

Attention! Response with potential to cause controversy

You are right, most people follow these "recommendations" because they have seen somewhere and they who are "teaching" other people also do not know why they do this. Some think they know.

In fact, the answers given in the rray link show that < in> getter and setter by itself can not be considered encapsulation. Just because you left a private field (and not attribute) does not mean that you are encapsulating something, especially when you put a < in> getter and setter straight to it without thinking about what it is doing.

I am increasingly convinced that getter / setter and other methods that serve as a buffer to access the real state is abstraction and not encapsulation. Okay, I still can not say it with such force and I know it will not be well accepted because the definition that is usual in our environment, and even is formalized says that this mechanism is the encapsulation. So I do not expect people to change their vision and I will not dwell on it. It seems to me that encapsulation is leaving everything that is necessary to the object in the object itself. If someone asks more about it, I can give you an answer.

What's the point of having a deprivation field and a couple of methods that normally terminates the field in the same way that it would be accessed directly? How can this be encapsulation?

I think it may be abstraction, because abstraction assumes that the concrete inner mechanism does not matter.

Encapsulation has to do with the fact that the behavior to change the state of the object is next to the object itself. Then there will be a method that will make the change or access to the value of the field. But nothing says it should be getter or setter .

But it can be too. If the only behavior to access / change the field is directly related to the state purely and simply, that's fine. The important thing for the encapsulation is that the behavior that allows the access / mutation of the field is in the same object.

There is a caveat that you should never have setters and maybe getters methods. These people say, and even give reason, that they should have methods that have them do certain actions that will manipulate the state in some way, but that is not so straightforward.

I disagree a little because there are many cases that the only thing you should do with the field is change its value, and in most cases to get some data just get the value of the field.

Of course, people abuse, even by not understanding how to apply this technique correctly. When a class is full of getters and especially setters it really should have something wrong.

To illustrate using the max motivator in the comments above: you should not have getSaldo() and setSaldo() , must have Depositar() and Sacar() , among other methods that can change the balance. Maybe getSaldo() makes sense, but a simple method name change can give another impression, something like ObtemSaldo() or PedeSaldo () '. You need to understand what an operation is for giving good names.

Note that the Sacar() method does much more than just tinkering with the balance, it is not a simple setter , apart from the truism that it does not change the balance directly, it will do a subtraction .

If you use a constructor properly you have a very low need to use setters . The validation here can be considered as an encapsulation as Piovezan said in its response, after all it is close to its object.

Often the right one to get information is to get a set and not just a single field, there would also enter the same idea that the method should not only return the state of a field. Of course, to do this will probably create a complication because you may have to create a class just to support the set of fields that will return in a given situation.

Still following Piovezan's response, even though he has used the term getXXX() in some method, I do not know if everyone can be considered as getters . If it returns a subset of a data collection, is this by itself no longer a method with a special meaning? Calling getter means that it has no function other than to give a value, has no specific semantics, it happens to be part of the object's mechanism and not its meaning.

Note that some languages provide better mechanism for working with these things, such as properties and tuples.

I know this is somewhat complicated to explain, abstractions are poorly understood by humans. I need to find a way to better explain mechanism and semantics.

  

I'm still going to finish

    
25.04.2018 / 02:23
3

There is no obligation for an getter and setter object for each attribute field. You have to create them only when you need them, and you do not even have to be the pair, you can be just one of them.

The article Encapsulation is not information hiding , which may interest you, cites the example of an object representing a geographic coordinate: latitude and longitude only allow certain ranges of values, and the encapsulation helps to set them within valid bounds. This is an example of setters that validates received values.

I do not use both setters , but I do a lot of validation on builders. If a parameter argument was incorrectly passed I already triggered for example a IllegalArgumentException : "You are trying to build the wrong object!" Also a form of encapsulation. p>

In real projects I've also used a getter that needed to return a copy of a collection (you know what it is, right? A class that stores elements, such as a list, a collection, and so on). Inside the object was a collection of immutable elements. I wanted to return a subset of this collection or a copy of it so that if the client code tried to modify the returned collection, it would not change my original collection. Modifying the elements was already guaranteed that it would not be possible, since these were immutable. I then made an getter that returned a copy or subset of the collection. Ready.

Another example: a getter that returned the connected (true or false) status of an alarm center. This hub encapsulated a Socket of Java and the connected information came from socket.isClosed() . Note that it is not a simple getCampo() or isCampo() . It does a processing, although minimal, which is to invert the returned value ( isClosed() returns if it is closed, I had a isConectada() that returns if the connection is open).

    
25.04.2018 / 02:05