What's wrong with my way of explaining DI and IoC?
Yesterday during my interview I was asked what is DI and IoC in spring. My answer was:
when a
class(A)
extends abstractclass(B)
or implements or instantiatesinterface(B)
an object ofclass(B)
any class in it then it isA
said to depend onB
. Injection of this dependency, that is, injection of an object into a costructor or in a setter method is called DI and in this process control over the creation of the object goes to the "outside world", like XML configurations, this inversion of control is IoC. DI is not required by IOC. We can still have a DI when there is no IOC.
The interviewer disagreed with me - where was I wrong?
One more thing -
As we used
Super class reference variable
eithercoding through interface
in a parameter of a constructor or setter method. Is this somehow related toDI
/IOC
, or is it just to achieveloose coupling
?
First operator *** "when class (A) extends abstract class (B) or implements interface (B)" ***
This is inheritance and cannot be injected as a dependency through Spring
The second operator "creates an object of class (B) of any class in it, then A is said to be said to depend on B"
It sounds good
"Including this dependency, that is, injecting an object into a costructor or setter method is called DI"
Incomprehensible expression to explain dependency injection. Here it is necessary to clarify the meaning of the injection. i.e. the dependency management is handled by the spring container, it controls their lifecycle and thus delegates / injects the classes that request them (via the spring config file)
"its control over the creation of the object goes to the" outside world ", for example, to XML configuration
Here, control does not go to the outside world or to the xml configuration file, but to the spring container. And spring container uses this config file to do its job.
"this inversion of IoC control. DI is not required by IOC. We can still have DI when there is no IOC.
Although no problem with this, but it seems incomplete. Here the IOC needs to be explained. Trying to explain it with an image
source to share
IoC ( I nversion o f C ontrol) is an abstract concept. This means that objects do not create dependent objects directly, they are obtained from outside the object.
There are several basic techniques for implementing control inversion.
- Using the factory pattern
- Using the service locator template
- Using Dependency Injection (DI) like
- Constructor input
- Entering parameters
- Installation node
- Enabling the interface
- Using contextual search
- Using the Design Patterns Patterns
- Using the Strategy Design Pattern
source to share
Your answer makes sense, and I share the same opinion with a slight variation.
The concept of IoC was originally heard in the era of procedural programming. Therefore, from the historical context, IoC talked about accessing the control- stream property , i.e. who owns the responsibility to call the function in the right order - whether it's the function itself or you have to invert it into some kind of external object.
However, after the advent of OOP, people started talking about IoC in an OOP context, where applications do the creation of objects and also manage the connections between objects, beyond the control- flow. Such applications wanted to invert ownership of object creation (rather than control- thread) and required a container that is responsible for object creation, object lifecycle dependencies, and application object injection, thereby excluding application objects from creating another specific object.
In this sense, DI is not the same as IoC, since it is not about control- flow, however, it is a kind of Io * , that is, the inverse of ownership of object creation.
I have discussed this topic here .
source to share