Course contentsShow
← Software Engineering
Lesson 1771 of 1,93351. Legacy Code and Brownfield DevelopmentFree lesson

Dealing with Entangled Dependencies

Strategies for untangling tightly-coupled legacy modules including interface extraction and adapter patterns.

Dealing with Entangled Dependencies

You'll learn how to untangle tightly-coupled legacy modules using interface extraction and adapter patterns.

The Problem of Entanglement

Legacy code often resembles a plate of spaghetti: modules that should be independent are instead tightly coupled, making changes risky and testing nearly impossible. When Class A directly instantiates Class B, which depends on Class C, which calls back into A, you have entangled dependencies—a circular mess that resists modification.

Think of it like trying to remove one sweater from a tangled pile: pulling on it just tightens the knots elsewhere.

Interface Extraction

The first untangling strategy is interface extraction: identify what a dependent class actually needs from another class, then create an interface representing only those needs. The dependent code now relies on the abstraction, not the concrete implementation.

This severs the tight coupling. You can now pass different implementations (including test doubles) without the dependent code knowing or caring. You've created a seam—a place where you can alter behavior without editing the dependent code itself.

Adapter Pattern

Sometimes you can't change how an entangled dependency works—perhaps it's a third-party library or touching it is too risky. The adapter pattern lets you wrap the problematic dependency behind an interface you control. Your code talks to the adapter, and the adapter translates to the legacy code.

This creates a buffer zone. You can gradually migrate to a better solution by swapping out the adapter's implementation while keeping the interface stable.

The Process

  1. Identify the specific coupling points using seams
  2. Extract an interface representing only what's needed
  3. Create an adapter if you can't modify the dependency directly
  4. Pass the interface/adapter instead of the concrete class
  5. Use characterization tests to ensure behavior is preserved

Key Takeaway: Interface extraction and adapter patterns break entangled dependencies by introducing abstractions at seams, allowing you to test and modify legacy code safely without requiring massive rewrites.