🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

Programming Concepts Intermediate

What is Dependency Injection?

A design pattern where objects receive their dependencies from external sources rather than creating them internally.

Dependency injection (DI) passes dependencies to a class instead of having the class create them. Instead of class UserService { db = new Database() }, you write class UserService { constructor(db) }. This makes code testable, flexible, and loosely coupled.

Types include constructor injection, setter injection, and interface injection. DI containers (Spring, Angular's injector) automate dependency resolution. DI is fundamental to testable code — mocking dependencies becomes trivial.

Related Terms

Memoization
An optimization technique that caches function results for given inputs, avoiding redundant computations for repeated calls.
Observer Pattern
A design pattern where an object (subject) maintains a list of dependents (observers) that are notified automatically of state changes.
Data Structure
A way of organizing and storing data in a computer so it can be accessed and modified efficiently.
Race Condition
A bug that occurs when the behavior of software depends on the timing or order of uncontrolled events like thread scheduling.
Factory Pattern
A creational design pattern that provides an interface for creating objects without specifying their exact classes.
Garbage Collection
An automatic memory management process that identifies and reclaims memory no longer in use by a program.
View All Programming Concepts Terms →