Contents
Meta
—
* still in an early stage
—
* still in an early stage
Modules should be encapsulated.
Information hiding and encapsulation are sometimes seen as one and sometimes as two separate but related notions2). This varies through literature. There are three stages of information hiding/encapsulation which can be defined as having a capsule, making the capsule opaque, and making the capsule impenetrable.
Having a capsule means that an object has methods which enable the client of the module to use it without accessing its internal data structures. Making the capsule opaque means that the inner workings are hidden from the clients. This is typically done by using access modifiers (private, protected). Lastly making the capsule impenetrable means that no client should be able to get a direct reference to an internal data structure.
A properly encapsulated module with an impenetrable capsule is better than an module with just an opaque capsule. And this is better than a module with a non-opaque capsule. But at least having a capsule is better than not having one at all.
When the inner workings of a module are hidden from the outside, then they can be changed without any other module noticing it. If the interface of the module stays the same, the rest of the system is not affected by the change. So adhering to IH/E prevents ripple effects.
Date
, Money
, EMailAddress
, TelephoneNumber
, etc. interface
to themSee section contrary principles.
David L. Parnas: On The Criteria To Be Used In Decomposing Systems Into Modules
protected
interface which can be used by subclasses. For these cases LSP has to be considered, too.
In Delphi there is the data structure TDateTime
which represents a specific date and time value 4). This is an alias name for a double value where the integer part represents the number of days since December 30, 1899 and the fractional part represents the time of day. This alone is a data structure but it is not encapsulated.
The Delphi runtime library (RTL) now specifies functions which operate on TDateTime
structures. This is “having a capsule”. But since it is still possible to access the internal representation directly, the inner workings are not hidden.
This is different in Java. Here the inner workings are hidden. It is not possible to access the private attributes of java.util.Date
5). Here the capsule is opaque (and impenetrable).
A typical example for an opaque but penetrable capsule is the following:
class SomeClass { private SomethingDifferent innerObject; public SomethingDifferent getInnerObject() { return innerObject; } }
In such a case the innerObject
is private, which means it is hidden. But it is revealed by the getter method. In order to establish an impenetrable capsule, the object has to be copied:
class SomeClass { private SomethingDifferent innerObject; public SomethingDifferent getInnerObject() { return innerObject.clone(); } }
Discuss this wiki article and the principle on the corresponding talk page.
Anbert Endres, Dieter Rombach: A Handbook Of Software And Systems Engineering
Paul Rogers: Encapsulation Is Not Information Hiding
Craig Larman: Applying UML and Patterns