User Tools

Site Tools


principles:information_hiding_encapsulation

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
principles:information_hiding_encapsulation [2013-06-15 15:31]
94.217.39.37 [Context]
principles:information_hiding_encapsulation [2013-06-22 20:41]
christian
Line 3: Line 3:
 ===== Variants and Alternative Names ===== ===== Variants and Alternative Names =====
  
-  * Parnas'​ Law((Anbert Endres, Dieter Rombach//A Handbook of Software and Systems Engineering//))+  * Parnas'​ Law(({{page>​resources:A Handbook of Software and Systems Engineering#​reference}}))
  
  
Line 15: Line 15:
  
 ===== Principle Statement ===== ===== Principle Statement =====
 +
 +Modules should be [[glossary:​encapsulation|encapsulated]].
  
  
 ===== Description ===== ===== Description =====
 +
 +Information hiding and encapsulation are sometimes seen as one and sometimes as two separate but related notions(({{page>​resources:​Encapsulation is not Information Hiding#​reference}})). 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 ===== ===== 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 [[glossary:​ripple effects]].
  
 ===== Strategies ===== ===== Strategies =====
Line 43: Line 52:
 ===== Origin ===== ===== Origin =====
  
-David Parnas//​[[http://​citeseerx.ist.psu.edu/​viewdoc/​summary?​doi=10.1.1.132.7232|On the Criteria To Be Used in Decomposing Systems into Modules]]//+{{page>​resources:On the Criteria To Be Used in Decomposing Systems into Modules#​reference}} 
 ===== Evidence ===== ===== Evidence =====
 /* Comment out what is not applicable and explain the rest: */ /* Comment out what is not applicable and explain the rest: */
-/* + 
-  * [[wiki:​Proposed]] +/*  * [[wiki:​Proposed]]*/ 
-  * [[wiki:​Examined]] +/*  * [[wiki:​Examined]]*/ 
-  * [[wiki:​Accepted]] + 
-  * [[wiki:​Questioned]] +  * [[wiki:​Accepted]]: Virtually every book on object-orientation (e.g. (({{page>​resources:​Applying UML and Patterns#​reference}})) explains IH/E to some extend. 
-*/+ 
 +/*  * [[wiki:​Questioned]]*/​ 
  
  
Line 82: Line 94:
 ===== Examples ===== ===== Examples =====
  
 +==== Example 1: Date and Time ====
 +
 +In Delphi there is the data structure ''​TDateTime''​ which represents a specific date and time value ((http://​docwiki.embarcadero.com/​Libraries/​XE3/​en/​System.TDateTime)). 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''​((http://​docs.oracle.com/​javase/​7/​docs/​api/​java/​util/​Date.html)). Here the capsule is opaque (and impenetrable).
 +
 +==== Example 2: Aliasing ====
 +
 +A typical example for an opaque but penetrable capsule is the following:
 +
 +<code java>
 +class SomeClass
 +{
 + private SomethingDifferent innerObject;​
 +
 + public SomethingDifferent getInnerObject()
 + {
 + return innerObject;​
 + }
 +}
 +</​code>​
 +
 +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:
 +
 +<code java>
 +class SomeClass
 +{
 + private SomethingDifferent innerObject;​
 +
 + public SomethingDifferent getInnerObject()
 + {
 + return innerObject.clone();​
 + }
 +}
 +</​code>​
  
 ===== Description Status ===== ===== Description Status =====
 /* Choose one of the following and comment out the rest: */ /* Choose one of the following and comment out the rest: */
-[[wiki:​Stub]]+/*[[wiki:​Stub]]*/
 /​*[[wiki:​Incomplete]]*/​ /​*[[wiki:​Incomplete]]*/​
-/*[[wiki:​Complete]]*/+[[wiki:​Complete]]
  
  
Line 98: Line 147:
   * [[wp>​Information hiding]]   * [[wp>​Information hiding]]
  
-  * [[http://​www.javaworld.com/​javaworld/​jw-05-2001/​jw-0518-encapsulation.html|Java World: Encapsulation is not information hiding]]+  * {{page>​resources:​Encapsulation is not Information Hiding#​reference}}
   * [[wiki>​EncapsulationIsNotInformationHiding]]   * [[wiki>​EncapsulationIsNotInformationHiding]]
  
   * [[wiki>​OnDecomposingSystems]]   * [[wiki>​OnDecomposingSystems]]
 +
 +===== Discussion =====
 +
 +Discuss this wiki article and the principle on the corresponding [[talk:​principles:​Information Hiding/​Encapsulation|talk page]].
principles/information_hiding_encapsulation.txt · Last modified: 2013-06-22 20:41 by christian