Injection injection is not a form of IoC. Inversion of Control is a template that is not associated with DI at all, except for the fact that they are usually used together in some kind of structure, which makes people think that they are the same when they are not.
Enabling a dependency simply means that you insert class dependencies into it, through the constructor or a series of setters, rather than creating them in the class. This can be done without any type of IoC container, completely by hand.
In fact, a simple example of manual DI might be:
import org.apache.http.client.HttpClient; public class TwitterClient { private HttpClient httpClient; public TwitterClient(HttpClient httpClient){ this.httpClient = httpClient; } }
Whenever you create TwitterClient in your code, you will also need to create an HttpClient and pass it. Since this would be rather tedious, there are frameworks to simplify it, but, as I said, it is quite possible to do this manually. This article discusses manual DI - http://googletesting.blogspot.com/2009/01/when-to-use-dependency-injection.html , and in fact, early versions of some Google products were built entirely around manual DI.
The advantage here is that you can swap implementations, so if you want to go through a cut-out client for unit testing, it will be easy for you. Otherwise, there would be no real way to unit test a class like this.
IoC means that you have some kind of structure that governs the life cycle of the application. A good example of an IoC that is not DI related would be just about any of the Cocoa frameworks that control the life cycle of a Cocoa application. You implement certain methods that are called at specific points in the application life cycle. That is why this is the "principle of Hollywood", you do not call the framework, the structure calls you.
mblinn
source share