DI provides objects that an object needs. So rather than the dependencies construct themselves they are injected by some external means. For instance let’s say we have the following below class “Customer” who uses a “Logger” class to log errors. So rather than creating the “Logger” from within the class, you can inject the same via a constructor as shown in the below code snippet.
The biggest benefit achieved by the above approach is “Decoupling”. You can now invoke the customer object and pass any kind of “Logger” object as shown in the below code.
Customer obj = new Customer(new EmailLogger());
Customer obj1 = new Customer(new EventViewerLogger());
You can also reads this awesome discussion Design pattern interview questions: – Which design patterns have you used in your project ?.
If you are java background read this 20 Java J2ee design pattern interview questions.
You can also visit our site for www.questpond.com for c# design pattern interview question videos.