Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What is dependency injection and how does it relate to design patterns? Dependency injection क्या है और यह design patterns से कैसे संबंधित है?

Answer

Dependency injection (DI) is a technique where an object's dependencies are provided from outside rather than created internally, achieving loose coupling and following the Dependency Inversion Principle.

// WITHOUT dependency injection - tight coupling
class EmailService {
    public void send(String message) {
        System.out.println('Email: ' + message);
    }
}

class NotificationServiceOld {
    private EmailService emailService = new EmailService();  // hardcoded, tightly coupled

    public void notify(String message) {
        emailService.send(message);
    }
}
// Problem: NotificationServiceOld can ONLY ever use EmailService,
// hard to test (can't substitute a mock), hard to swap implementations

// WITH dependency injection - loosely coupled via an interface
interface MessageService {
    void send(String message);
}

class EmailServiceImpl implements MessageService {
    public void send(String message) {
        System.out.println('Email: ' + message);
    }
}

class SmsServiceImpl implements MessageService {
    public void send(String message) {
        System.out.println('SMS: ' + message);
    }
}

class NotificationService {
    private final MessageService messageService;

    // Constructor injection - dependency provided from OUTSIDE
    public NotificationService(MessageService messageService) {
        this.messageService = messageService;
    }

    public void notify(String message) {
        messageService.send(message);
    }
}

// Client decides WHICH implementation to inject
NotificationService emailNotifier = new NotificationService(new EmailServiceImpl());
NotificationService smsNotifier = new NotificationService(new SmsServiceImpl());

emailNotifier.notify('Hello');  // Email: Hello
smsNotifier.notify('Hello');    // SMS: Hello

// Easy to test with a mock implementation
class MockMessageService implements MessageService {
    public void send(String message) {
        System.out.println('Mock sent: ' + message);  // no real email/SMS sent in tests
    }
}

// Frameworks like Spring automate this wiring using @Autowired,
// managing object creation and injection so you rarely write 'new' manually

Dependency injection (DI) एक technique है जहां object की dependencies बाहर से provide की जाती हैं, internally create नहीं होतीं, loose coupling achieve करता है और Dependency Inversion Principle follow करता है।

// DI के बिना - tight coupling
class EmailService {
    public void send(String message) {
        System.out.println('Email: ' + message);
    }
}

class NotificationServiceOld {
    private EmailService emailService = new EmailService();  // hardcoded

    public void notify(String message) {
        emailService.send(message);
    }
}

// DI के साथ - interface से loosely coupled
interface MessageService {
    void send(String message);
}

class EmailServiceImpl implements MessageService {
    public void send(String message) {
        System.out.println('Email: ' + message);
    }
}

class SmsServiceImpl implements MessageService {
    public void send(String message) {
        System.out.println('SMS: ' + message);
    }
}

class NotificationService {
    private final MessageService messageService;

    public NotificationService(MessageService messageService) {
        this.messageService = messageService;
    }

    public void notify(String message) {
        messageService.send(message);
    }
}

NotificationService emailNotifier = new NotificationService(new EmailServiceImpl());
NotificationService smsNotifier = new NotificationService(new SmsServiceImpl());

emailNotifier.notify('Hello');
smsNotifier.notify('Hello');

// Spring जैसे frameworks @Autowired से यह wiring automate करते हैं

Was this answer clear?