Sunday, May 12, 2013

DEPENDENCY INVERSION (D INV)

To put it straight, DI = IoC + D Inv. How? Lets explore.
Before the helper and DI were introduced, Hands/Legs/Face were designed first. When Body was created it just used new Hands/Legs/Face. Body does not dictate the design of Hands or Legs. Suppose Hands were designed to have teeth. Body can’t dictate that Hands should not have Teeth (High level module can’t interfere with design of Low level module, it can just use it). There are there and Body has to live with it. So everyone will eat off their hands, literally.
Programming wise Body is High Level Component and Hands are Low Level Component and are kept in separate packages. Body Component Package depended upon Hand Component Package for compilation. Thus HLC depends upon LLC for compilation.
DInv says other wise. Low Level Components (Hands) also need to confirm  to some abstraction (Hands have to be type of SomeHands, so now we have SmallHands, ThinHands and also TeethHands which are type of SomeHands, but NOT all Bodies need to have TeethHands). Body is also bound by SomeHands. It can’t fit ShortLeg instead of SomeHands.
DInv says both Low Level and High Level modules need to confirm to some abstraction.
Programming wise, this is achieved by separating High Level component  (Body) and Low Level component  (Hands) into separate packages. And the Interfaces (SomeHands, SomeLegs) defining the Behavior/Services which the HLC needs are inside HLC package and are owned by it. Here LLC implement the interfaces owned by HLC. Thus LLC depend on HLC for compilation. Thus usual Dependency relationship is INVERTED.