Chapter 3. Information / implementation hiding

Two views: public view (from the outside), private view (inside). Private view should be suppressed from others.

information hiding: information within cannot be seen from the outside. Implementation hiding: implementation details cannot be perceived from the outside.

Important

Information / Implementation hiding

is the use of encapsulation to restrict from external visibility certain information or implementation decisions that are internal to the encapsulation structure.

Back to the example from the book: hominoid: hides some information, example: direction. can be changed form the outside, but cannot be read. (however, can be displayed, and we may be able to check the position) -> information hiding.

However, encapsulation goes beyons that: can reveal information but hide implementation: Variable inside does not need to be implemented the same way as the attribute.

Example: hominoid reveals locatin via location attribute, but how is it stored internally? could be (xCoord, yCoord) or (yCoord, xCoord), polar coordinates or some scheme the designer dreamed up at 1 am. However: location attribute exports the location in a specific form. We don't care how the information is stored. This is implementation hiding.

The direction is an example implementation and information hiding. Direction could be degrees (0-359), characters (N, E, S, W), percentDirection (0.00.. 99.999), or anything else.

In a redesign, if we decide to release direction, we have to chose how to release it, however, this is independent from the actual implementation.

implementation / information hiding provides a black-box view. External object have view of what the object can do, but no knowledge how.

Figure 3.1. Black Box View
Black Box View

Benefits:

Localizes design decisions: Private design decision have little or no impact on the rest of the system. Local decision can be made and changed without impact on the rest of the system. This limits "ripple of change" effect.

Decouples content of information from its form of representation. No information internal is tied to external format. this prevents external users / programmers from meddling. if also prevents unstable connections.

Visibility Modifiers

Visibility modifiers describe who can see and / or modify / execute an attribute or operation.

Notation in UML: shown with the visibility modifier:

  • private ( - )

  • public ( + )

Special attribute property {readOnly} to indicate read-only attributes.

Example 3.1. Public, private, and read-only attributes

Practice: implement atomic bomb.

Example 3.2. Combination of students results

However, actual implementation is different!