Law of Demeter#

The Law of Demeter, also known as the Principle of Least Knowledge, is a software development principle that promotes loose coupling and encapsulation. It guides developers in designing and writing code that limits dependencies between objects, reducing the impact of changes and improving maintainability.

The Law of Demeter states that an object should only communicate with its immediate neighbors and avoid interacting with objects that are further down the hierarchy. In other words, an object should have limited knowledge of other objects and should only interact with those that are directly associated with it.

Here are some key aspects of the Law of Demeter for developers:

  1. Limited Knowledge: An object should only have knowledge of its own attributes, its immediate dependencies, and objects it creates or receives as method parameters. It should not have detailed knowledge of complex internal structures or the internal workings of other objects.

  2. Limited Coupling: Objects should communicate with other objects through a limited number of well-defined interfaces. They should avoid calling methods of unrelated or indirectly related objects, as it introduces unnecessary coupling and makes the code more fragile.

  3. Encapsulation: Objects should encapsulate their internal state and behavior, exposing a minimal set of public methods to interact with the outside world. This encapsulation helps protect the internal implementation and allows for better control over changes and modifications.

  4. Law of “Tell, Don’t Ask”: Instead of querying the internal state of an object and then making decisions based on that information, the Law of Demeter encourages objects to delegate responsibility and ask other objects to perform actions on their behalf. This promotes a more object-oriented and collaborative design.

By following the Law of Demeter, developers can create code that is more modular, flexible, and maintainable. It reduces the impact of changes by limiting the knowledge and dependencies between objects, allowing for easier modifications and updates. It also improves code readability and makes it easier to reason about and test individual components.

In summary, the Law of Demeter encourages developers to design code that respects the principle of least knowledge, limits coupling between objects, and promotes encapsulation. By adhering to this principle, developers can create more robust, maintainable, and loosely coupled systems.

In other words:

Talk to friends but not to friends of friends.

or translated to software development:

Talk to your own methods but not to methods of other objects.

Violation of the LoD#
 class Money:
     def __init__(self, amount: float):
         self.amount = amount

 class Wallet:
     def __init__(self, money: 'Money'):
         self.money = money

 class Person:
     def __init__(self, wallet: 'Wallet'):
         self.wallet = wallet

 # Violation of Law of Demeter
 john = Person(Wallet(Money(100.0)))
 print(john.wallet.money.amount)

Corrected version

Adhering to the LoD#
 class Money:
     def __init__(self, amount: float):
         self.amount = amount

 class Wallet:
     def __init__(self, money: 'Money'):
         self.money = money

     def get_money_amount(self) -> float:
         return self.money.amount

 class Person:
     def __init__(self, wallet: 'Wallet'):
         self.wallet = wallet

     def get_wallet_money_amount(self) -> float:
         return self.wallet.get_money_amount()

 # Adhering to Law of Demeter
 john = Person(Wallet(Money(100.0)))
 print(john.get_wallet_money_amount())

References#