Encapsulation of the data

The encapsulation of the data is a rule of microarchitecture (detailed architecture) consisting in hiding the data of a class or a module to the other classes or modules, i.e., to prevent the access to the data by another means that methods (functions members). Consequently, the interface of a class or a module obeying this principle never exposes these members of data like variables, paintings or structures but only of the methods (functions members).

Implementation

All the directed computer programming languages objects like Java or C++ offer limiters of access (levels of visibility) making it possible to easily implement the principle of encapsulation of the data. The traditional limiters are:
  • public : the methods (functions members) of all the other classes or modules can reach the data having the level of public visibility. It is about low the level of data protection.

  • protected : the access to the protected data is reserved to the methods (functions members) classes heiresses. It is about an intermediate level of data protection.

  • private : the access to the private data is limited to the methods (functions members) class owner. It is about the highest level of data protection.

Unfortunately, no computer programming language directed object obliges the programmer to protect the data members, in other words, it is always possible to declare them public. It is about an anti-owner of design which very good programmer avoids at all costs and this for several reasons.

Motivation

  1. the encapsulation makes it possible to change the structures of data of a module or a class without modifying the interface of this one and thus without modifying the classes and modules which use it. This situation frequently arrives when one wants to increase the effectiveness (speed of treatment) of a class or a module, it is often necessary to modify the structures of data consequently.
  2. the encapsulation makes it possible to easily add rules of validation and constraints of integrity like, for example, to limit the field of the values which a variable can take (validity) or check that this value does not enter in conflict with the values of other variables (integrity).
  3. more generally, the encapsulation makes it possible to inform the class which a change with its data is about to occur. If this information becomes possibly crucial, the programmer not having encapsulated the data will be in front of a dead end.
  4. the encapsulation avoids the flat Antipattern of spaghetti which does not make it possible to determine which, it what and it how of a modification of data. Indeed, the systematic application of the encapsulation imposes a weak Couplage and thus prevents the strong Couplage, by common space or contents, person in charge of the flat of spaghetti.
  5. Finally, the encapsulation makes it possible to offer a directed interface services and responsibilities, i.e., to offer to the users (programmers, abstractionnists and architects) of the class or module an interface indicating clearly which services are offered and which are the responsibilities for this class or modulates.

See too

Random links:Nikolaus Harnoncourt | Turigi | Borgo | Group the Dispatch | Member of honor of a Rotary drill club