When designing software, maintainability, modularity, and low coupling are critical. One principle that helps achieve this in object-oriented programming is the Law of Demeter (LoD). Despite its formal name, the concept is simple: “only talk to your immediate friends.”
1. What is the Law of Demeter?
The Law of Demeter, also known as the Principle of Least Knowledge, is a guideline for reducing coupling between classes. It states:
A method of an object should only call methods of:
- Itself
- Its own fields (member objects)
- Objects passed as arguments
- Objects created locally within the method
In simple terms:
A class should only interact with objects it directly knows, not objects retrieved from other objects.
2. Why Law of Demeter Matters in LLD
In Low-Level Design (LLD), LoD is crucial because it:
- Reduces tight coupling between classes
- Makes the system easier to maintain
- Simplifies unit testing
- Encourages encapsulation, hiding internal details from other classes
3. Example: Violating the Law of Demeter
class Engine {
public void start() { System.out.println("Engine started"); }
}
class Car {
private Engine engine = new Engine();
public Engine getEngine() { return engine; }
}
class Driver {
public void drive(Car car) {
car.getEngine().start(); // ❌ Violates LoD
}
}
Problem:
Driver
knows too much about Car
’s internals (the Engine
).
This creates tight coupling — any change in Car
’s engine structure could break Driver
.
4. Correct Implementation (Following LoD)
class Engine {
public void start() { System.out.println("Engine started"); }
}
class Car {
private Engine engine = new Engine();
public void startEngine() { engine.start(); } // LoD-friendly interface
}
class Driver {
public void drive(Car car) {
car.startEngine(); // ✅ Only talks to immediate friend
}
}
Benefits:
-
Driver
only interacts withCar
, notEngine
. -
Car
encapsulates itsEngine
internally. - Changes inside
Engine
do not affectDriver
.
5. Common Patterns to Apply Law of Demeter
- Encapsulation: Keep fields private and provide public methods for necessary actions.
- Facade Pattern: Use a higher-level interface to interact with complex subsystems.
-
Avoid method chaining: Don’t write code like
a.getB().getC().doSomething();
- Dependency Injection: Pass objects as arguments to methods rather than accessing nested objects directly.
6. Summary Table
Principle | Description | Benefit |
---|---|---|
Law of Demeter (LoD) | Only call methods of immediate friends | Reduces coupling, increases modularity |
Violation | Accessing sub-objects of sub-objects | Tight coupling, harder to maintain |
7. Key Takeaways
- LoD is all about reducing unnecessary dependencies between classes.
- Following it makes your code cleaner, more modular, and maintainable.
- In LLD, it ensures modules interact in a controlled, predictable manner.
- Always think: “Am I talking to my immediate friend, or someone I just met through another friend?”
✅ Conclusion:
The Law of Demeter is a simple yet powerful principle. When applied consistently, it prevents tight coupling, encourages encapsulation, and results in robust, maintainable systems.
Top comments (0)