Search This Blog

Saturday, October 13, 2007

What is Dependency Injection Pattern?

1 The idea behind dependency injection (a.k.a. Inversion of Control, IOC) is that each application component declares what type of service objects it requires (a.k.a its dependency).

2 The container resolves the dependency between application components, instantiates service objects, and then injects service stubs into the application at runtime via automatic Java Bean setter calls or direct data field value assignment.

3 Dependency Injection proposes separating the implementation of an object and the construction of objects that depend on them.

4 The job of coordinating the implementation and construction is left to the Assembler code. The object that needs to be implemented does not need to instantiate the dependent objects and can rely on the assembler to do the job.

5 The assembler will gather and instantiate, if necessary, all the dependent objects and make them available to the implemented object.

6 Since the assembler does not depend on the code directly changes can be made to the assembler without any changes to the code.

7 The assembler gathers the required classes through configuration files so a change in the assembler only needs changes to the configuration file. In this case the Assembler code would be the spring framework.

No comments: