liveBook · Manning

Loose coupling

As explained in Spring Start Here, Second Edition

Decoupling is the reduction of direct dependencies between software components so that one component's implementation can change independently of another's.

Loose coupling means components depend on what another component does, not on its specific implementation. Like booking a trip without choosing the driver or vehicle, a component can request a service without knowing who provides it. section 4.1.1

Changing how delivery details are sorted

A DeliveryDetailsPrinter that calls SorterByAddress directly must change if sorting switches to sender name. Instead, make it depend on a Sorter interface. Different sorting classes can implement that contract, so the printer can keep using the service without knowing which implementation provides it. section 4.1.1

In Spring, dependency injection can supply a bean that implements the interface. If several beans implement it, use @Primary for a default or @Qualifier to select one. This keeps the dependency explicit while leaving the implementation replaceable. section 4.5

Figure 4.3 Using an interface to decouple the responsibilities. Instead of depending directly on an implementation, the DeliveryDetailsPrinter object depends on an interface (a contract). DeliveryDetailsPrinter can use any object implementing this interface instead of being stuck to a specific implementation.

See how the printer depends on the Sorter contract rather than a particular sorting class. section 4.1.1

How Spring Start Here, Second Edition covers it

How deep it goes
Develops the idea through a Java sorting example and Spring injection choices, without detailing interface internals.
Examples
Uses a ride-sharing analogy and a delivery printer whose sorting strategy changes from destination address to sender name.
What you can do after
Build Java components that depend on an interface and receive an implementation through Spring dependency injection.
How it connects
Connects loose coupling to Java interfaces, dependency injection, Spring beans, and aspect-oriented programming.

Also explained in

Compare how 7 books explain loose coupling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.