I frequently get the term “Dependency Injection” in a AS3 book i am reading. However, it’s has not explained what exactly it is. May anyone pls exhibit what exactly it is using some lines of AS3.0 code ?
Thanks
Vishwas
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Dependency Injection simply means that instead of a class deciding what objects it needs in order to work it gets passed those objects from outside.
In normal development, our objects decide their classes concretely, like this:
Moving to Dependency Injection,
MyClassnow gets its dependencies from external:In that example, we pass in the
SomeObjclass when constructingMyClass. You can also use public variables/properties either. Passing objects through the constructor is good for vital dependencies.Now to make this actually useful, we can use interfaces instead of concrete classes. This lets us change the behaviour of
MyClasseasily:To give you an example, the
ISomeObjinterface might describe a renderer. TheSomeObjImplAclass will render using the normaladdChild()techniques, while theSomeObjImplBclass will render using blitting. Now, you can change how you render yourMyClassobject simply by changing a parameter – theMyClassclass doesn’t need to know if it’s being rendered normally or through blitting.The object that decides what class to pass to
MyClassis what’s called an injector. How it decides which class to inject is where you get the flexibility. For example, you could load a XML object that contains the classes to inject, or use reflection, which is how Robotlegs works I think.The link that 32bitkid posted is a pretty good explanation of the principle, but that’s the gist of it. You can also check out the wiki page. For bonus points, read about Inversion of Control, which is somewhat related.