Meeting 11

Aus Java Student User Group Austria - Java + JVM in Wien Österreich / Vienna Austria
Version vom 16. Dezember 2012, 06:16 Uhr von 36.73.61.92 (Diskussion) (rGDMMtDCGUSixczY)
Wechseln zu: Navigation, Suche

Cedric, probably most of your cnsufoion stems from the fact that the vast majority of documentation and tutorials on IoC that are available on the web use awful examples that do nothing to illustrate IoC's advantages. The referenced tutorial from your blog entry is an excellent case in point it's a rather poor example of IoC.I myself use a home-grown IoC container at work, which is used mostly to auto-wire services together. In that vein IoC is merely convenient, not revolutionary or anything. In a nutshell, you don't want to care where services come from or how they get instantiated, or even whether they're singletons or not. On some level of course you do have to know this but you _don't_ want to know this inside your dependent class. You also don't want to have to know which factory to use. In fact, when you're using IoC for the most part when you're developing you don't know and don't care where a given component comes from.So on one level it's like the difference between push and pull. You can write code to pull service references, by having them instantiate those objects, use a factory to get them, or use a lookup mechanism to find them. Or you can use an IoC container and have those self-same references pushed to you when you need them.You don't really feel the IoC difference looking at small examples, because they're too small to matter either way. Where you feel it is in larger software projects. The vast majority of times you're writing new code or maintaining old code, and you'll need to get a reference to some service . And, most of the time, when using an IoC container this means you have to do nothing more than add it to your constructor or add a setter for it and you're done. The rest happens automatically via auto-wiring.This is true at least for the simple cases where you're referring to a class which is a singleton (and the only implementation of that type). In more complex cases you may have to do slightly more such as adding in an ID in an XML config or some Java code to distinguish which service you want. But even in those cases the fundamental point remains you indicate declaratively what you want, and the correct instance gets pushed to you. Ultimately, that's what IoC is all about.