You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/encapsulation.tex
+99-14Lines changed: 99 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -9,15 +9,16 @@
9
9
\chapter{Encapsulation}
10
10
11
11
\begin{introduction}
12
-
Encapsulation is one important concept of OOP.
12
+
Encapsulation is an important concept of OOP.
13
13
It is the mechanism of restricting direct access to some members of a class.
14
14
It helps managing complexity when debugging source code.
15
15
If a field is accessed everywhere in your source code, this makes it difficult to find errors related to this field.
16
16
Also, if a field of a class (A) is used by a class (B) then we changed it (either its name or how it gets assigned), we have to change all the code where it appears in (B).
17
17
In this chapter, we will show different visibility modes and how they are implemented in each language.
18
-
You will see that there are two views: "Many programmers are irresponsible, dim-witted, or both." and "Programmers are responsible adults and capable of good judgment".
18
+
You will see that there are two views: ``Many programmers are irresponsible, dim-witted, or both." and ``Programmers are responsible adults and capable of good judgment".
19
19
So, a programming language may fall into one or another.
20
20
\end{introduction}
21
+
Some languages have just public fields, and it is up to programmers to decide if they want to access it directly.
21
22
22
23
\section{Public members}
23
24
@@ -55,7 +56,7 @@ \subsection{Java}
55
56
56
57
Every public member in Java must preceded by the keyword \keyword{public}.
You can find some hacks on the web, but they can be expensive in term of memory and processing power.
183
-
To emulate such mechanism, you have to know the caller object
201
+
To emulate such mechanism, you have to know the caller object.
184
202
If you insist on having protected and private members, some libraries can be helpful such as \nameword{mozart}\footnote{mozart: \url{https://github.com/philipwalton/mozart}}.
Perl can capture the caller (the location or package where a function was called) using the keyword \keyword{caller}.
214
+
Also, the base class \keyword{UNIVERSAL} provides a method \keyword{isa} which returns true if the object is blessed to the given type or inherits from it.
215
+
Adding to it the keyword \keyword{\_\_PACKAGE\_\_} which captures the current package and exception management \keyword{die}, Bingo! we can limit access.
216
+
We cannot limit visibility: the method is still visible, but we raise an exception if the access does not serve our purpose.
There is a convention to use underscore \keyword{\_} as a mean to signal a class member as private.
295
335
But the member still accessible as public.
296
-
Another way is to use closures (the combination of a function and the lexical environment within which that function was declared), by defining a field inside the constructor using the keyword \keyword{var} or \keyword{let}.
336
+
Another way is to use closures (\textit{the combination of a function and the lexical environment within which that function was declared}), by defining a field inside the constructor using the keyword \keyword{var} or \keyword{let}.
297
337
But, any method using this variable must be defined inside the constructor.
298
-
This means each time you create a new instance (object), a new function will be created for this object.
338
+
This means each time you create a new instance (object), a new method will be created for this object.
If you want a protected access, just modify the setter/getter to do so.
394
+
In addition, if you want to prevent modifying the content of a protected field from a subclass, just delete the setter code block.
395
+
396
+
\subsubsection{Using closures}
397
+
398
+
This solution is what you will find on the web. %ref to https://www.perlmonks.org/?node_id=8251
399
+
Instead of using hashes as objects, you may implement them as closures.
400
+
A closure is a subroutine reference that has access to the lexical variables that were in scope when it was created.
401
+
The idea is to define a subroutine that will act as an setter/getter method and bless it into the class instead of the hash.
402
+
The solution presented here is a simplified one, with some restrictions on closure.
403
+
Here I forbid access outside the class to make all fields private, then you can create second hand setters/getters for those you want to grant access to.
0 commit comments