Encapsulation is the ability we give our classes (and objects created
from them) to hide the implementation details of their inner workings and
control the consistency of the internal state and invariants.
It happens all the time that we expose some attribute in our class to the
outside world in the form of a collection like the following:
This is actually pretty common and I think a bad practice, we tend to create
private fields in our classes and as a reflex create setters and getters
immediately without thinking about the consequences of exposing the state that
way.
If we simply return the reference to the attribute of the object there is a
lot of danger there because now others have a copy of the reference to that
attribute that is supposed to be private and under the control of the owner
object.
Imagine if some code that gets that list of emails from our class now tries to
modify the content of that list by adding or removing some objects from it.
How can we control that? what if there is some kind of limit on the number of
emails a Contact can have? How can we ensure only valid emails are added?
What if, instead, we write the following:
Now users of our class will get a list of emails that cannot be modified. New
Email objects cannot be added or removed (but still the objects in the list
can be changed if the public methods in the Email class allow that 😉). You
could also create a copy of the list of emails by instantiating a new
ArrayList sending the list of emails as an argument to the constructor
new ArrayList<>(this.emails)
but I still think it is better to fail when trying to modify the list.
Probably it is even better to just expose public methods with just the exact
functionality that we want to allow like the following:
With the last implementation, we are hiding more and then have even more
control. For example, let's say a Contact can have thousands of Emails (I know, it is unlikely, but let's say that is possible), then we can even change internally
the way we store Emails from a List to a Map and then be able to have better
performance for some operations, like removing an Email.
Do you have suggestions on how to improve the information hiding here? please comment!