Home > Java > javaTutorial > Law of Demeter (LoD) Explained in Seconds

Law of Demeter (LoD) Explained in Seconds

DDD
Release: 2025-01-24 22:32:10
Original
722 people have browsed it

The Law of Demeter (LoD): Keep Your Code Loosely Coupled

The Law of Demeter (LoD) is a design guideline aimed at reducing coupling in your code. Its core principle is simple: "Only talk to your immediate friends, not to strangers."

In essence, a class or module should only interact directly with objects it depends on, avoiding interactions with objects those objects depend on. This promotes simpler, more testable, and less interconnected code.

Law of Demeter (LoD) Explained in  Seconds

Anti-Pattern (Avoid):

<code>// Tight coupling through nested calls
customerCity := order.GetCustomer().GetAddress().GetCity()
fmt.Printf("Customer lives in: %s\n", customerCity)</code>
Copy after login

This example demonstrates tight coupling. Changes to the Order, Customer, or Address classes could break this code.

Improved Approach:

<code>// Decoupled using a single method call
customerCity := order.GetCustomerCity()
fmt.Printf("Customer lives in: %s\n", customerCity)</code>
Copy after login

The GetCustomerCity() method encapsulates the complexity, hiding the internal structure and reducing dependencies.

Benefits of LoD:

  • Reduced Coupling: Easier to maintain and modify code with fewer interdependencies.
  • Improved Readability: Code becomes more straightforward and easier to understand.
  • Information Hiding: Internal implementation details are shielded from external components.
  • Easier Testing: Simplifies testing by reducing the need for complex mocks.

Applying LoD in Practice:

  • Employ Data Transfer Objects (DTOs) to manage data flow.
  • Utilize Facade patterns to simplify interactions with complex subsystems.
  • Refactor chained method calls into single, higher-level methods.

Further Exploration:

Interested in learning more about software design principles? Explore these related concepts:

  • Dependency Inversion Principle (DIP)
  • Golang Dependency Injection
  • Interface Segregation Principle (ISP)
  • You Aren't Gonna Need It (YAGNI) Principle
  • Liskov Substitution Principle (LSP)
  • Keep It Simple, Stupid (KISS) Principle
  • Don't Repeat Yourself (DRY) Principle
  • Tell, Don't Ask Principle

Connect with me on LinkedIn, GitHub, and Twitter/X for updates on future posts.

The above is the detailed content of Law of Demeter (LoD) Explained in Seconds. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template