Dependency Injection (DI) vs Service Locator Pattern?

Updated Apr 28, 2026

Short answer

Both decouple classes from their dependencies, but DI pushes dependencies in, while Service Locator pulls dependencies out.

Deep explanation

In DI, dependencies are explicitly declared in the constructor. In Service Locator, the class explicitly asks a global locator registry for its dependencies. Service Locator hides dependencies (making code hard to test and reason about) and is largely considered an anti-pattern today.

Unlock with a Pro subscription to view this section.

View pricing

Real-world example

No real-world example available yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

Common mistakes

No common mistakes listed yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

Follow-up questions

No follow-up questions available yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

More Design Patterns interview questions

View all →