Definition:
The Law of Demeter (also known as the Principle of Least Knowledge) is a design guideline that states an object should only communicate with its immediate friends and not with strangers. It promotes loose coupling by limiting the knowledge an object has about other objects.
**Core Rule:“Don’t talk to strangers”
Example:
// GOOD: Following LoD - direct communication only
public class OrderProcessor {
public void processOrder(Customer customer) {
// Let customer handle its own business
String city = customer.getCityName();
double tax = customer.getTaxRate();
}
}
public class Customer {
private Address address;
// Customer knows how to get its city name
public String getCityName() {
return address.getCityName();
}
// Customer knows how to get tax rate
public double getTaxRate() {
return address.getTaxRate();
}
}