If Object A depends on object B, IoC is to remove its dependency.
Example 1. Need to hardcode 'Circle' or 'Triange' for object creation.
Circle c = new Circle()
c.draw();
Triange t = new Triange();
t.draw();
Example 2. POLYMORPHISM, but still hardocded to use Circle or Triange
Aplication class knows which one to use.
Shape c = new Circle()
c.draw();
Shape t = new Triange();
t.draw();
Example 3. Create a method to pass a generic type (parent class/interface)
TRUE POLYMORPHISM, DYNAMIC, LOOSELY COUPLEDBut still somewhere in the class the object should to be created and passed to method1.
method1(Shape shape) {
shape.draw();
}
Example 4. Create a separate class in which object is not created.
CLASS INDEPENDENT OF THE USAGE
class Drawing {
private Shape shape;
public setShape(Shape shape){
this.shape = shape;
}
public drawShape(){
this.shape.draw();
}
}
// Dependency injected in some other class
Triange triange = new Triange();
drawing.setShape(triange);
drawing.drawShape();