Table of Contents

Information Hiding/Encapsulation (IH/E)

Variants and Alternative Names

Context

Principle Statement

Modules should be encapsulated.

Description

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.

Rationale

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.

Strategies

Caveats

See section contrary principles.

Origin

Evidence

Relations to Other Principles

Generalizations

Specializations

Contrary Principles

Complementary Principles

Principle Collections

OOD Principle Language
General Principles
ML KISS MIMC DRY GP RoE
Modularization Principles
MP HC ECV
Module Communication Principles
TdA/IE LC DIP
Interface Design Principles
EUHM PLS UP
Internal Module Design Principles
IH/E IAP LSP PSU

Examples

Example 1: Date and Time

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.Date5). Here the capsule is opaque (and impenetrable).

Example 2: Aliasing

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();
	}
}

Description Status

Complete

Further Reading

Discussion

Discuss this wiki article and the principle on the corresponding talk page.

1)

Anbert Endres, Dieter Rombach: A Handbook Of Software And Systems Engineering