Skip to main content

The Law Of Demeter

The Law of Demeter, also known as the Principle of Least Knowledge, is a design principle in object-oriented programming that emphasizes the importance of reducing the coupling between objects. The idea is that objects should only communicate with their immediate neighbors and not reach out to other objects that are not directly related to their functionality.

The principle is based on the idea that objects should have a limited knowledge of other objects in the system. In other words, an object should only know about its own properties and the properties of the objects it interacts with directly. This helps to reduce the complexity of the system and makes it easier to maintain and modify.

The Law of Demeter is typically expressed as the "dot rule," which states that an object should only communicate with its immediate neighbors and not with objects further down the chain. For example, if object A wants to get information from object C, it should not call a method on object B, which then calls a method on object C. Instead, object A should call a method on object B, which then returns the necessary information to object A.

The Law of Demeter is an important principle in software design because it helps to reduce the coupling between objects, making the system more modular and easier to maintain. By following this principle, developers can create more flexible and robust systems that are easier to modify and extend over time.