Wednesday, April 20, 2011

Design pattern: DP in action - Inversion of Control

Applying the IoC pattern to a class means removing the creation of all object instances for which this class isn't directly responsible and passing any needed instances instead.
The instances may be passed using a specific constructor, using a setter, or as parameters of the methods needing them. It becomes the responsibility of the calling code to correctly set these domain objects on the called class.
Extreme Programming guru Ron Jeffries explains it:
My car has a diagnostic port and an oil dipstick. There is an inspection port on the side of my furnace and on the front of my oven. My pen cartridges are transparent so I can see if there is ink left.
And if I find it useful to add a method to a class to enable me to test it, I do so. It happens once in a while, for example in classes with easy interfaces and complex inner function (probably starting to want an Extract Class).
I just give the class what I understand of what it wants, and keep an eye on it to see what it wants next.

No comments :

Post a Comment