Dependency injection nghĩa là cung cấp dependency của một object qua tham số thay vì object tự tạo bên trong.
Lợi ích: test dễ hơn (truyền mock dependency), loose coupling, linh hoạt khi đổi implementation.
Ví dụ: thay vì val database = Database.getInstance() bên trong class, hãy truyền vào: class MyClass(val database: Database). Code modular và testable hơn nhiều.
Dependency injection means providing an object's dependencies through parameters instead of the object creating them internally.
Benefits: easier testing (pass mock dependencies), loose coupling, flexibility to change implementations. For example, instead of val database = Database.getInstance() inside the class, pass it: class MyClass(val database: Database). This makes code modular and testable.