Skip to content

Commit 8c37a11

Browse files
No code samples for Law of Demeter
1 parent cfe7d49 commit 8c37a11

File tree

1 file changed

+0
-47
lines changed

1 file changed

+0
-47
lines changed

design-patterns/law-of-demeter.md

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,3 @@
66

77
## Mnemonic for the Law of Demeter
88
The post [Visualization Mnemonics for Software Principles](http://www.daedtech.com/visualization-mnemonics-for-software-principles) by [Erik Dietrich](https://github.com/erikdietrich) provides a very effective trick to understand (and never forget anymore) what the Law of Demeter is. An Italian translation is available [here](https://github.com/arialdomartini/mnemonics/blob/law-of-demeter/README-italian.md).
9-
10-
## Code Example
11-
An example of code violating the Law of Demeter is provided by [Krzysztof Grzybek](https://github.com/krzysztof-grzybek)
12-
13-
```javascript
14-
class Plane {
15-
constructor(crew) {
16-
this.crew = crew;
17-
}
18-
19-
getPilotsName() {
20-
this.crew.pilot.getName();
21-
}
22-
}
23-
24-
class Crew {
25-
constructor(pilot) {
26-
this.pilot = pilot;
27-
}
28-
}
29-
30-
class Pilot {
31-
getName() {
32-
// ...
33-
}
34-
}
35-
```
36-
37-
It's bad, because it creates tight coupling between objects - they are dependent on internal structure of other objects.
38-
39-
Fixed code:
40-
```javascript
41-
class Plane {
42-
getPilotsName() {
43-
this.crew.getPilotsName();
44-
}
45-
}
46-
47-
class Crew {
48-
constructor(pilot) {
49-
this.pilot = pilot;
50-
}
51-
52-
getPilotsName() {
53-
return this.pilot.getName();
54-
}
55-
}

0 commit comments

Comments
 (0)