Inversion Of Control in layman's terms

This is a question oft asked by beginners, when they are inquisitive about why we use inversion of control and what bearing IOC has in the real world. So here I have attempted to explain the IOC with my own example:

Say there are three objects in real world :
1. A showroom that  does some value added services(eg like adding license plate and give the finishing touch by adding varnish) for cars and scooters and uses both for sale after doing the value add.
2. Scooter( which is the finish good created by Scooter Factory)
3. Car(which is the finished good created by the car factory)

Now if the showroom has to do finishing for the scooter(without using IOC), it should know what kind of scooter it is going to do Value added services for i.e. the specifications of the scooter and car(like color,length and the showroom would provide the specifications and get it from the scooter factory and the car factory. So the details of the car/ scooter have to be known to the showroom. Hence, there is a strong dependency/ coupling on showroom for the both of these objects. 

If you look at it programmatically 
to create car object and scooter object from the Car class(ie the Car factory in real world) and Scooter class, the  showroom will have to know about the constructor arguments of car and scooter.

Class Showroom{
Scooter s;
Car c;
/* See how scooter and car are instantiated by calling constructor of class Scooter and Car.
    That means, Class Showroom needs knowledge about Scooter and Car like
    ‘Which are the possible constructors of Scooter and Car?
     What should be the constructor parameters?’
     This is termed as Tight coupling between Showroom and Scooter/showroom and Car/Showroom.
*/
Public Showroom(){
s = new Scooter(color, length, width);
car = new Car(color, length, width);
}
}

Now we do not want the showroom to necessarily know all the details of the scooter and car as it is only going to do value added services like adding license plate and give the finishing shine by adding varnish. So we would rely on some other entity , say Dealer,to get the scooter and the car from the Scooter factory and Car factory and pass the goods to the showroom to do the finishing. So the control of getting the car and scooter lies in the hands of the Dealer and the showroom knows nothing about the specifications of the car and the scooter it is going to do the finishing for until the car and scooter come to it. 
In the programming world this dealer (who passes on the goods to the showroom) would be called "Container". So all the details of the scooter and car are known only to the Container/dealer who has now become the Controller, previously the showroom was the controller. Hence the name inversion of control.

If you look at this programmatically,

Class Showroom{
Scooter s;
Car c;
/* Look Object of Scooter and Car will be created by Container and provided as Constructor argument.
So, Class Showroom doesn’t need to know class Scooter and Car constructors and all.
It is loose coupling between Showroom  and Scooter/Showroom  and Car.
*/
Public Showroom(Scooter s1, Car c1){
this.s = s1;
this.c = c1;
}
}

Thus, in the above example
the classes Showroom , Car and Scooter are less coupled. This method of passing the objects through a construtor is called constructor injection.

(I originally published this answer at https://www.quora.com/Computer-Programming/How-will-you-explain-Inversion-of-Control-in-laymans-terms) 

2 comments: