mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2026-09-24 15:00:31 +08:00
Add content: Law of Demeter in Software Design roadmap
Added comprehensive explanation of Law of Demeter principle with examples. Fixed @article tag capitalization. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+34
-1
@@ -1 +1,34 @@
|
||||
# Law of Demeter
|
||||
# Law of Demeter
|
||||
|
||||
Also called “Principle of Least Knowledge”, it states:
|
||||
|
||||
A method should only interact with its immediate dependencies, not deeply nested objects.
|
||||
|
||||
## In Practice
|
||||
|
||||
- Avoid chaining calls deep into the internals of other objects.
|
||||
- Restrict communication to objects you directly manage.
|
||||
|
||||
### 🔹 ❌ Bad Example (Violation)
|
||||
|
||||
```
|
||||
// Controller
|
||||
total = order.customer.address.getRegionTaxRate() * order.amount
|
||||
```
|
||||
|
||||
### 🔹 ✅ Good Example
|
||||
|
||||
```
|
||||
// Controller
|
||||
total = order.calculateTotal()
|
||||
```
|
||||
|
||||
## 🔹 Why It Matters
|
||||
|
||||
- **Reduces coupling** → fewer dependencies between classes.
|
||||
- **Increases maintainability** → changes in one class don’t affect distant classes.
|
||||
- **Improves readability** → clear boundaries of responsibility.
|
||||
|
||||
## 🔹 Resources
|
||||
|
||||
- [@Article: Law of Demeter Explained](https://en.wikipedia.org/wiki/Law_of_Demeter)
|
||||
|
||||
Reference in New Issue
Block a user