principles:information_hiding_encapsulation
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
principles:information_hiding_encapsulation [2021-03-25 10:32] – old revision restored (2021-03-24 20:02) 95.216.172.193 | principles:information_hiding_encapsulation [2021-09-02 12:36] – old revision restored (2021-05-19 09:29) 65.21.179.175 | ||
---|---|---|---|
Line 2: | Line 2: | ||
===== Variants and Alternative Names ===== | ===== Variants and Alternative Names ===== | ||
+ | |||
+ | * Parnas' | ||
===== Context ===== | ===== Context ===== | ||
/* fill in contexts here: */ | /* fill in contexts here: */ | ||
- | * [[contexts: | + | * [[contexts: |
+ | * [[contexts: | ||
+ | * [[contexts: | ||
===== 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 ===== | ||
+ | |||
+ | * Use the lowest possible visibility for a variable or method | ||
+ | * Make all attributes private and use getter and setter methods to access them | ||
+ | * Better also avoid getters and setters | ||
+ | * Find suitable abstractions for data types and use appropriate methods instead of just getters and setters | ||
+ | * Avoid aliasing problems with value objects | ||
+ | * If the programming language supports that use call-by-value objects (like stack objects in C++, structs in C#, records in Delphi, etc.) for value objects like '' | ||
+ | * Otherwise use immutable objects which are handled call-by-reference but needn' | ||
+ | * Avoid aliasing problems with lists and similar data structures | ||
+ | * Copy internal list objects before returning them or only return a read-only '' | ||
+ | |||
+ | |||
+ | ===== Caveats ===== | ||
+ | |||
+ | See section [[#contrary principles]]. | ||
===== Origin ===== | ===== Origin ===== | ||
+ | {{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: | ||
+ | |||
===== Relations to Other Principles ===== | ===== Relations to Other Principles ===== | ||
Line 47: | Line 80: | ||
* [[Model Principle]] (MP): IH/E demands having an interface for a module which hides the inner workings. MP tells how such an interface can look like. | * [[Model Principle]] (MP): IH/E demands having an interface for a module which hides the inner workings. MP tells how such an interface can look like. | ||
* [[Liskov Substitution Principle]] (LSP): For subclasses you can waken encapsulation by having a wider '' | * [[Liskov Substitution Principle]] (LSP): For subclasses you can waken encapsulation by having a wider '' | ||
- | | + | * [[Tell, don't Ask/ |
- | | + | * [[Low Coupling]] (LC): Higher forms of couplings (especially content couplings) break encapsulation. |
- | * [[Low Coupling]] (LC): | + | * [[Principle of Separate Understandability]] (PSU): |
- | * [[Principle of Separate Understandability]] (PSU): | + | * [[Easy to Use and Hard to Misuse]] (EUHM): A module should be properly encapsulated in order to make it easy to use and hard to misuse. |
==== Principle Collections ==== | ==== Principle Collections ==== | ||
Line 58: | Line 92: | ||
- | ===== Example | + | ===== 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: |
===== Further Reading ===== | ===== Further Reading ===== | ||
+ | * [[wiki> | ||
+ | * [[wp> | ||
+ | |||
+ | * [[wiki> | ||
+ | * [[wp> | ||
+ | |||
+ | * {{page> | ||
+ | * [[wiki> | ||
+ | * [[wiki> |
principles/information_hiding_encapsulation.txt · Last modified: 2021-10-18 21:56 by christian