- Published on
Inversion of Control
- Authors
- Name
- Lucas Xu
- @xianminx
Inversion of Control (IoC)
Hollywood Principle:
Don't call us, we'll call you.
Framework v.s. Library
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.
-- Ralph Johnson and Brian Foote
Example
JUnit FrameworksetUp
and tearDown
Lightweight containers
J2EE
Dependency Injection
- Constructor Injection
- Setter Injection
- Interface Injection
private MutablePicoContainer configureContainer() {
MutablePicoContainer pico = new DefaultPicoContainer();
Parameter[] finderParams = { new ConstantParameter("movies1.txt") };
pico.registerComponentImplementation(MovieFinder.class, ColonMovieFinder.class, finderParams);
pico.registerComponentImplementation(MovieLister.class);
return pico;
}
PicoContainer
NanoContainer: XML configure mapping
PicoContainer
Spring
Avalon
Interface Injection