principles:information_hiding_encapsulation
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
principles:information_hiding_encapsulation [2013-06-15 15:31] – [Context] 94.217.39.37 | principles:information_hiding_encapsulation [2021-10-18 21:56] (current) – +++ restored +++ christian | ||
---|---|---|---|
Line 3: | Line 3: | ||
===== Variants and Alternative Names ===== | ===== Variants and Alternative Names ===== | ||
- | * Parnas' | + | * Parnas' |
Line 15: | Line 15: | ||
===== Principle Statement ===== | ===== Principle Statement ===== | ||
+ | |||
+ | Modules should be [[glossary: | ||
===== Description ===== | ===== Description ===== | ||
+ | |||
+ | Information hiding and encapsulation are sometimes seen as one and sometimes as two separate but related notions(({{page> | ||
+ | |||
+ | 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: | ||
===== Strategies ===== | ===== Strategies ===== | ||
Line 43: | Line 52: | ||
===== Origin ===== | ===== Origin ===== | ||
- | David Parnas: // | + | {{page> |
===== Evidence ===== | ===== Evidence ===== | ||
/* Comment out what is not applicable and explain the rest: */ | /* Comment out what is not applicable and explain the rest: */ | ||
- | /* | + | |
- | * [[wiki: | + | /* * [[wiki: |
- | * [[wiki: | + | /* * [[wiki: |
- | * [[wiki: | + | |
- | * [[wiki: | + | * [[wiki: |
- | */ | + | |
+ | /* * [[wiki: | ||
Line 82: | Line 94: | ||
===== Examples ===== | ===== Examples ===== | ||
+ | ==== Example 1: Date and Time ==== | ||
+ | |||
+ | In Delphi there is the data structure '' | ||
+ | |||
+ | The Delphi runtime library (RTL) now specifies functions which operate on '' | ||
+ | |||
+ | This is different in Java. Here the inner workings are hidden. It is not possible to access the private attributes of '' | ||
+ | |||
+ | ==== 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; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In such a case the '' | ||
+ | |||
+ | <code java> | ||
+ | class SomeClass | ||
+ | { | ||
+ | private SomethingDifferent innerObject; | ||
+ | |||
+ | public SomethingDifferent getInnerObject() | ||
+ | { | ||
+ | return innerObject.clone(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
===== 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: | + | /*[[wiki: |
/ | / | ||
- | /*[[wiki: | + | [[wiki: |
Line 98: | Line 147: | ||
* [[wp> | * [[wp> | ||
- | * [[http:// | + | * {{page> |
* [[wiki> | * [[wiki> | ||
* [[wiki> | * [[wiki> | ||
+ | |||
+ | ===== Discussion ===== | ||
+ | |||
+ | Discuss this wiki article and the principle on the corresponding [[talk: | ||
+ |
principles/information_hiding_encapsulation.1371303089.txt.gz · Last modified: 2013-06-15 15:31 by 94.217.39.37