Which of the following best describes the Law of Demeter in system design?
Think about limiting the knowledge a module has about other parts of the system.
The Law of Demeter encourages modules to only talk to their immediate collaborators, reducing dependencies and improving maintainability.
In the following design, which interaction violates the Law of Demeter?
Class A calls a method on Class B, which returns an object of Class C. Then Class A calls a method on Class C.
Consider which call crosses multiple object boundaries in one step.
Law of Demeter discourages calling methods on objects returned by other methods (friends of friends). Class A calling Class C's method violates this.
You are designing a large-scale e-commerce system. To keep the system maintainable, how should you apply the Law of Demeter when components need to access nested data?
Think about how to hide internal structure and reduce dependencies.
Providing higher-level methods encapsulates internal details and prevents components from reaching deep into nested objects, following the Law of Demeter.
What is a common tradeoff when strictly enforcing the Law of Demeter in a complex system?
Consider what happens when you hide all internal details strictly.
Strict Law of Demeter can cause many small wrapper methods that forward calls, making code longer and sometimes harder to maintain.
Given the following code snippet, which refactoring best aligns with the Law of Demeter?
class Order {
Customer customer;
public String getCustomerCity() {
return customer.getAddress().getCity();
}
}Think about which class should hide the internal structure of its data.
Adding getCity() in Customer hides Address details from Order, reducing coupling and following the Law of Demeter.