BolEnglishLearn · Play · Win
Login

🔌 Software Design Principles · Core Principles

🔌

Loose Coupling

Design PrincipleIntermediateModule 2

Components should know as little as necessary about each other's internals.

Changing one component should not force many unrelated changes.

💡 In practice

OrderService uses an IPaymentService interface instead of vendor-specific code everywhere.

🖼️ CONCEPT PICTURE
A picture to help remember Loose Coupling
OrderService uses an IPaymentService interface instead of vendor-specific code everywhere.

💡 Like a wall socket: any appliance plugs in through the same shape, so you can swap the lamp without rewiring the house.

💡 Learn🛠️ Apply🎯 Check

💡 Why it matters

Tightly coupled code means a change in one place ripples into many others, making every edit risky. When components depend on small contracts instead of each other's details, you can replace, test or upgrade one piece without disturbing the rest. Loose coupling is what lets a big system keep evolving safely.

🛠️ How to achieve it

1
Depend on contracts
Have components talk through an interface that describes what, not how, so the details can change freely.
2
Hide the internals
Expose only what callers truly need and keep the rest private.
3
Inject dependencies
Pass in the things a component needs instead of letting it create them itself, so you can swap them.
4
Avoid reaching through
Do not let one component dig into another's inner objects; talk to the top-level contract only.

⚠️ Signs of trouble

  • •A small change forces edits across many unrelated files.
  • •Vendor-specific details are hard-coded throughout the app.
  • •You cannot test a component without its real neighbors.
  • •Classes know far too much about each other's internals.
⚠️ Common mistake: Hard-wiring a specific vendor or class everywhere, so swapping it means touching the whole codebase. Depend on a small interface and inject the concrete piece.

🎯 Quick check

Score: 0/3

Three questions to test the idea.

  1. 1. What makes components loosely coupled?

    Answer: They depend on small contracts, not each other's internals

    Talking through a contract lets one piece change without forcing changes in the others.

  2. 2. Why use an interface like IPaymentService instead of vendor code everywhere?

    Answer: So you can swap the vendor without touching the whole app

    Depending on the interface means the vendor detail lives in one place and is easy to replace.

  3. 3. What is a sign of tight coupling?

    Answer: A small change forces edits across many unrelated files

    Ripple-effect changes across many files show components know too much about each other.

🔑 Key terms

Coupling
How much one component depends on another.
Interface
A contract describing what a component offers, not how.
Dependency injection
Passing a component its collaborators instead of it creating them.
Encapsulation
Hiding internal details behind a clean outer surface.

✅ Review checklist

📊 Your progress

0%
Mark this topic learned to track it.
Saved in this browser.
🚀 Got it?
Mark it learned and move to the next topic.
Next topic →