design patterns - What is Inversion of Control? How does that relate to dependency injection? -


possible duplicates:
difference between dependency injection (di) & inversion of control (ioc)
inversion of control < dependency injection

hey, scott hanselman interview question. find question hard answer. may parts of question answered on stack on whole important.

i know other forms of ioc apart di.

can explain me real time examples.

thanks

dependency injection not form of ioc. inversion of control pattern that's not related di @ all, except fact they're used in sort of framework, lead people think they're same thing when they're not.

dependency injection means inject class's dependencies it, through constructor or series of setters, rather instantiating them in class. can done without ioc container of sort, manually.

a simple example of manual di be:

import org.apache.http.client.httpclient;   public class twitterclient {      private httpclient httpclient;      public twitterclient(httpclient httpclient){         this.httpclient = httpclient;     } } 

whenever create twitterclient in code, have create httpclient , pass in. since rather tedious, there frameworks make easier, mentioned it's totally possible manually. article touches on manual di - http://googletesting.blogspot.com/2009/01/when-to-use-dependency-injection.html, , in fact versions of google produces built entirely around manual di.

the benefit here can swap out implementations, if wanted pass in stubbed-out client unit testing purposes it's easy. otherwise, there no real way unit test class that.

ioc means you've got sort of framework that's in control of application's lifecycle. example of ioc doesn't involve di of cocoa apple frameworks, control lifecycle of cocoa app. implement methods called @ points in lifecycle of app. that's why it's "hollywood principle", don't call framework, framework calls you.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -