0
0
LLDsystem_design~5 mins

Law of Demeter in LLD - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the Law of Demeter?
The Law of Demeter is a design guideline that suggests a module should only communicate with its immediate friends and not with strangers. It means an object should only call methods of: itself, its fields, parameters, or objects it creates.
Click to reveal answer
beginner
Why is the Law of Demeter important in system design?
It reduces dependencies between modules, making the system easier to maintain and less fragile. Changes in one part of the system are less likely to break others.
Click to reveal answer
intermediate
Which of the following violates the Law of Demeter?<br>
order.getCustomer().getAddress().getCity()
This violates the Law of Demeter because it accesses a chain of objects beyond immediate friends. The object should not reach through multiple objects to get data.
Click to reveal answer
intermediate
How can you fix a violation of the Law of Demeter?
You can add methods to intermediate objects to hide internal structure. For example, add a method getCity() in Customer that returns the city, so callers don't chain calls.
Click to reveal answer
beginner
What is a real-life analogy for the Law of Demeter?
Imagine you want to know the weather. Instead of asking your friend’s friend’s friend, you ask your friend directly. Your friend then asks their friend and tells you the answer. You don’t talk to strangers.
Click to reveal answer
Which of these is a principle of the Law of Demeter?
AUse global variables for communication
BAccess any object in the system freely
CTalk only to your immediate friends
DChain multiple method calls across objects
What problem does the Law of Demeter help to avoid?
ALow performance
BHigh coupling between modules
CMemory leaks
DSyntax errors
Which code snippet violates the Law of Demeter?
Auser.getProfile().getEmail()
Buser.sendMessage()
Corder.calculateTotal()
Dcart.addItem(item)
How can you improve code that violates the Law of Demeter?
AAdd methods to hide internal object details
BIncrease the number of chained calls
CUse global variables instead
DRemove all method calls
The Law of Demeter is also known as:
ADon't Repeat Yourself
BOpen/Closed Principle
CSingle Responsibility Principle
DPrinciple of Least Knowledge
Explain the Law of Demeter and why it is useful in designing software systems.
Think about how objects should limit their knowledge about others.
You got /4 concepts.
    Describe a scenario where violating the Law of Demeter causes problems and how you would refactor it.
    Consider how to hide internal object structure.
    You got /4 concepts.