Pages

Monday, July 2, 2012

Inversion of Control / Dependency Injection


A simple way to decouple classes from what they depend on

"The Framework of Code Classes" calls me rather than me calling the Framework"
This phenomenon is Inversion of Control (also known as the Hollywood Principle - "Don't call us, we'll call you").


EXAMPLE:  Lets say we have a CD Player that takes CD's

Dependency Injection

One important characteristic of a framework is that the methods defined by the user to tailor the framework will often be called from within the framework itself, rather than from the user's application code.

The framework often plays the role of the main program in coordinating and sequencing application activity.
This inversion of control gives frameworks the power to serve as extensible skeletons.
The methods supplied by the user tailor the generic algorithms defined in the framework for a particular application.

Inversion of Control is a key part of what makes a framework different to a library.
A library is essentially a set of functions that you can call, these days usually organized into classes.
Each call does some work and returns control to the client.
A framework embodies some abstract design, with more behavior built in.
In order to use it you need to insert your behavior into various places in the framework either by subclassing or by plugging in your own classes.
The framework's code then calls your code at these points.
















No comments:

Post a Comment