Skip to main content
Of Hollywood, frameworks, APIs and inversion of control

I recently chanced to learn about a pattern called the 'Inversion of Control'. This is primarily used to abstract out the creational part of dependent components from a class is question. In simple terms, a class need not create dependent objects. This independence of the creational logic allows such classes to be more loosely coupled to dependent objects, thus resulting in easy replacement of the dependent objects during run-time. This technique is used by unit-tests to isolate a particular class from its dependencies so that the class / object in question can be tested alone.

We usually resort to using creational patterns like the singleton or the factories to decouple the creator from the logic of creation. What we essentially do is, we just add one more level of indirection to the actual creation logic. Earlier, we used to manually use new to create an object. With the singleton, our class uses the singleton, which in turn hosts the construction logic. In essence, the dependency is still there, separate by an additional layer of code.

If we truly wanted to remove the dependency, then delegate the dependency logic to the parent or the caller. A way to do this is simply pass the object to depend upon via the constructor or via a setter. The parent or the caller now has the responsibility to create the dependent object and associate it to our object in question during run-time. This technique is useful where unit-tests can instate our class and pass on mock dependent objects to test the class / component in question in isolation.

To sum up, the control for creation is inverted, from the class in question to the parent or caller.

Of frameworks and API then?

One key differentiator between a framework and an API is, your class or object or application calls the API when needed (e.g Win32 API, STL, Bost++ etc). You have the control. Frameworks work the other way round. The framework is the application and your code fills in the missing pieces. The framework calls your routines. The framework has the control to call your code when it needs (OWL, MFC etc). The framework has the control. Moving the control from your class to an external entity is called the inversion of control.

What of Hollywood?

"Don't call us; we'll call you" - the standard line most of the promising artists would have heard, and the same applies to software too, where inversion of control is marked as a synonym to the Hollywood principle.

Is it good then?

With the promising trend on unit testing and its benefits, it definitely looks like inversion of control is a good investment when developing your code so that your developers can develop unit-tests to quickly tests your classes.

Comments

Popular posts from this blog

Inside a Text Editor Ever since my college days, after dabbling with vi and a few other editors, I always had an yearning to create my own. Now, I am still stuck with XEmacs and jEdit and had a chance to compile / study the sources and documentation of EMACS and a free editor component called Scintilla. Until now, I was under the the belief that text editors used a doubly linked list to represent the text in memory. The advantages of this approach being insertions and deletions are much more easier which is just a matter of just un-linking a node off the list. But the shortcomming is that they tend to fragment memory with each node or line take a bit of memory. The other alternative approach is to have a dynamic array which is a contiguous space of memory and can sometimes be directly written off to a file. The disadvantages are that insertion and deletion are costly and you need to reallocate quite frequently. While goint throug the source and documentation of text editors, I chanced ...
Battle of Wesnoth Been on the lookout for a free turn based strategy game and chanced upon the Battle of Wesnoth . Despite it being an open source game (meaning, you get the source), it was incredibly polished akin to any of the other turn based strategy game (Alpha Centauri), be it the background score or the graphics or the tutorials. The game itself is set in a period similar to the D&D or nethack era. For the film buffs, if you have read or seen the Lord of the Rings, you would probably be able to relate to the clans that populate the game world. The game play, as with any turn based strategy game requires background information on each of the units that you own, their strengths and weaknesses and a lot of planning (a kin to chess, but with a lot more parameters) where factors like day - night cycles are taken into account (e.g, humans fight well during the day, but the orcs are better during the night). It is encouraged to keep your older units as they gain experience and beco...

The Age of the Hybrid?

Honda recently offered a slash in their new Civic Hybrid model. Though not all are sure if it was a publicity move or an inventory clearing sale, I believe with good mileage figures and a growing concern on how green everyone should be, hybrid cars will see an increase in prominence in the Indian roads. The Indian market is price sensitive. Be it an industrialist who is buying a Rolls Royce or a car enthusiast pining for a BMW, the inevitable question remains: "What is the mileage?". This has led to a slew of Diesel cars in the market . Though economic to us, it still fails against the 'green' parameter. The other competitor to hybrids are the all-electric vehicles which may be termed greener (assuming that the cost of production, battery is far less harmful than the actual emissions), they lack in range, ability to quickly recharge (which seems to be a 5 minute affair for it's counterparts) and the lack of accessible recharge locations (do you think at the curren...