0Pricing
Clean Architecture & Design Patterns in Practice · Lektion

Die Dependency Rule erklärt

Verstehen Sie die grundlegende Dependency Rule: Abhängigkeiten dürfen nur nach innen fließen, damit die zentrale Geschäftslogik isoliert bleibt.

Die Dependency Rule erklärt ist eine kostenlose Clean Architecture & Design Patterns in Practice-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Clean Architecture & Design Patterns in Practice-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Clean Architecture & Design Patterns in Practice-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

The Core of Clean Architecture

Welcome to our lesson on the Dependency Rule! This principle is the cornerstone of Clean Architecture, ensuring your core business logic remains isolated and independent.

It's all about directing the flow of dependencies in your application in a very specific way.

Dependencies Flow Inwards

The fundamental idea is simple: dependencies can only flow inwards. This means outer layers of your architecture must depend on inner layers, but inner layers must never depend on outer layers.

Think of it as a one-way street, always leading towards the center of your application's logic.

Visualizing the Rule

Recall the concentric circles of Clean Architecture (Entities, Use Cases, Interface Adapters, Frameworks/Drivers). The Dependency Rule dictates that code in an outer circle can depend on code in an inner circle, but never the other way around.

  • Inner layers: Core business rules, independent.
  • Outer layers: UI, databases, web frameworks, external services.

Why This Rule Matters

Strictly following the Dependency Rule brings significant benefits:

  • Isolation: Your core business logic is protected from changes in UI, databases, or frameworks.
  • Testability: You can test your core logic without needing to set up a database or a web server.
  • Flexibility: You can swap out external components (e.g., change from SQL to NoSQL, or from one web framework to another) with minimal impact on your core.

Outer Layer Calling Inward

This is the natural and allowed flow. An outer layer, like a web controller, depends on an inner layer, like a Use Case, to perform an action. The controller knows about the use case.

Try running this example:

class CreateUserUseCase {
  public void execute(String username) {
    System.out.println("Logic to create user: " + username);
  }
}

// Outer layer (e.g., Web Controller)
class UserController {
  private final CreateUserUseCase createUserUseCase;

  public UserController(CreateUserUseCase useCase) {
    this.createUserUseCase = useCase;
  }

  public String register(String username) {
    createUserUseCase.execute(username);
    return "User registration initiated for " + username;
  }
}

public class Main {
  public static void main(String[] args) {
    CreateUserUseCase useCase = new CreateUserUseCase();
    UserController controller = new UserController(useCase);

    System.out.println(controller.register("Alice"));
  }
}

Inner Layer Independence

Now, what if our CreateUserUseCase (inner layer) needs to send a success message back to the UserController (outer layer)?

It cannot directly call a method on the UserController. That would mean the inner layer depends on the outer layer, breaking the Dependency Rule!

Inverting the Dependency

To solve this, we use Dependency Inversion. The inner layer (Use Case) defines an interface (often called a 'Port' or 'Output Port') that it expects to communicate through.

The outer layer (Controller/Presenter) then implements this interface and provides itself to the inner layer. This reverses the flow of dependency at compile time.

Code: Inverted Dependency

Observe how the CreateUserUseCase now depends only on its own interface, UserOutputPort. The UserController implements this port, allowing the Use Case to 'talk back' without knowing the specific UI details.

Try running this example:

// 1. Inner layer defines its 'output port' (interface)
interface UserOutputPort {
  void presentUserCreationResult(String message);
}

// 2. Inner layer (Use Case) depends on its own interface
class CreateUserUseCase {
  private final UserOutputPort outputPort;

  public CreateUserUseCase(UserOutputPort outputPort) {
    this.outputPort = outputPort;
  }

  public void execute(String username) {
    // Simulate user creation logic
    System.out.println("Processing creation for: " + username);
    outputPort.presentUserCreationResult("User '" + username + "' created!");
  }
}

// 3. Outer layer (e.g., Web Controller) implements the 'port'
class UserController implements UserOutputPort {
  private final CreateUserUseCase createUserUseCase;

  public UserController() {
    // Controller provides itself as the output port
    this.createUserUseCase = new CreateUserUseCase(this);
  }

  public void registerUser(String username) {
    createUserUseCase.execute(username);
  }

  @Override
  public void presentUserCreationResult(String message) {
    System.out.println("UI Update: " + message);
  }
}

public class Main {
  public static void main(String[] args) {
    UserController controller = new UserController();
    controller.registerUser("Bob");
  }
}

Consequences of Breaking the Rule

Violating the Dependency Rule leads to a fragile and rigid architecture:

  • Tight Coupling: Your core logic becomes tied to external details (UI, DB).
  • Difficult Testing: Testing business rules requires setting up external components.
  • Reduced Flexibility: Changing frameworks or databases becomes a massive undertaking.
  • Leaky Abstractions: Inner layers expose knowledge of outer layers, making the system harder to understand and maintain.

Quick Check

The Dependency Rule is crucial for maintaining a clean and flexible architecture. Let's test your understanding of how dependencies should flow.

Recap: The Dependency Rule

You've learned that the Dependency Rule is the core principle of Clean Architecture, dictating that dependencies must always flow inwards, from outer layers to inner layers.

  • It protects your core business logic.
  • It enhances testability and flexibility.
  • Dependency Inversion (using interfaces) is key to allowing inner layers to communicate outwards without direct dependencies.

Mastering this rule is essential for building robust and maintainable software systems!

Häufig gestellte Fragen

Ist die Lektion „Die Dependency Rule erklärt“ kostenlos?

Ja — der vollständige Text von „Die Dependency Rule erklärt“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Clean Architecture & Design Patterns in Practice-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Clean Architecture & Design Patterns in Practice-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Die Dependency Rule erklärt“?

Verstehen Sie die grundlegende Dependency Rule: Abhängigkeiten dürfen nur nach innen fließen, damit die zentrale Geschäftslogik isoliert bleibt. Du übst Clean Architecture & Design Patterns in Practice mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Clean Architecture & Design Patterns in Practice zu starten?

Keine Vorkenntnisse erforderlich. Clean Architecture & Design Patterns in Practice auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Die Dependency Rule erklärt“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Clean Architecture & Design Patterns in Practice-Lektion Code schreiben und ausführen?

Ja. Jede Clean Architecture & Design Patterns in Practice-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Was ist Clean Architecture?
  2. Architekturebenen verstehen
  3. Die Dependency Rule erklärt
  4. Screaming Architecture und Use-Case-Intention
← Zurück zu Clean Architecture & Design Patterns in Practice