Dependency Inversion Principle<br />By DunithDhanushka<br />
Agenda<br />Walkthrough of a real world scenario.<br />What is Inversion of Control?<br />What is Dependency Injection?<br...
SOLID Principles<br />Single Responsibility Principle<br />Open Closed Principle<br />Lizkov’s Substitution Principle<br /...
Meet Joe the developer!!<br />
Joe’s contract is to build a IMDB clone!<br />
First cut of the Movie Lister app<br />
Problems with it<br />Can’t extend the application without modifying the existing code.<br />Violates Open/Closed principl...
Introduction of Interfaces<br />
New Movie Lister App<br />
Issues of new Movie Lister<br />MovieLister class has to talk to both interface and implementation!!<br />This makes Movie...
How can we fix this???<br />
Its IT! We got remedy! ;)<br />Using Setters ( Use a setter to set a Finder instance for MovieLister)<br />Passing a Finde...
Using a setter<br />
What really happened there?<br />MovieLister depends on the Finder<br />Previously, MovieLister took the control of initia...
This makes MovieLister<br />Share across other developers.<br />Makes portable.<br />Encourages unit testing.<br />Preserv...
Ways to achieve IoC<br />Dependency Injection<br />Dependency Inversion Principle<br />A. High-level modules should not de...
Methods of DI<br />Constructor Injection<br />Setter Injection<br />Interface Injection<br />
IoC Containers<br />How does an IoC container works?<br />
Popular IoC containers for Java<br />Pico Container<br />Spring<br />JBoss Micro kernel<br />Google Guice<br />OSGi ( sort...
Joe managed to save his head!<br />Joe used Spring container.<br />He implemented the MovieLister as a Spring bean.<br />I...
Thank you!<br />We’ll meet in the next week with more Spring examples!<br />Adios!<br />
Upcoming SlideShare
Loading in...5
×

An Introduction to Dependency Inversion Principle

1,584
-1

Published on

Introduces what is Inversion of Control, Dependency Injection and How they relates to the Dependency Inversion Principle

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,584
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
15
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

An Introduction to Dependency Inversion Principle

  1. 1. Dependency Inversion Principle<br />By DunithDhanushka<br />
  2. 2. Agenda<br />Walkthrough of a real world scenario.<br />What is Inversion of Control?<br />What is Dependency Injection?<br />Multiple forms of Dependency Injection<br />IoC Containers<br />R.I.P Steve Jobs (1955 -2011)!<br />
  3. 3. SOLID Principles<br />Single Responsibility Principle<br />Open Closed Principle<br />Lizkov’s Substitution Principle<br />Interface Segregation Principle<br />Dependency Inversion Principle<br />
  4. 4. Meet Joe the developer!!<br />
  5. 5. Joe’s contract is to build a IMDB clone!<br />
  6. 6. First cut of the Movie Lister app<br />
  7. 7. Problems with it<br />Can’t extend the application without modifying the existing code.<br />Violates Open/Closed principle!!! :S<br />
  8. 8. Introduction of Interfaces<br />
  9. 9.
  10. 10. New Movie Lister App<br />
  11. 11. Issues of new Movie Lister<br />MovieLister class has to talk to both interface and implementation!!<br />This makes MovieLister less portable.<br />This makes MovieLister less reusable.<br />Switching implementations requires a code change! :@<br />Hard to unit test.<br />
  12. 12. How can we fix this???<br />
  13. 13. Its IT! We got remedy! ;)<br />Using Setters ( Use a setter to set a Finder instance for MovieLister)<br />Passing a Finder instance as constructor argument for MovieLister<br />Use a Factory of Finders for MovieLister<br />Use a ServiceLocator<br />
  14. 14. Using a setter<br />
  15. 15. What really happened there?<br />MovieLister depends on the Finder<br />Previously, MovieLister took the control of initiating it’s dependency.<br />But in new version, dependency has been instantiated by some other party for the MovieLister.<br />This is called “Inversion of Control” (IoC)<br />
  16. 16. This makes MovieLister<br />Share across other developers.<br />Makes portable.<br />Encourages unit testing.<br />Preserves OCP (Movie Lister doesn’t know any thing about its crappy implementers. Also plugging a new Finder doesn’t require a code change)<br />Enables the use of Mock Objects.<br />
  17. 17. Ways to achieve IoC<br />Dependency Injection<br />Dependency Inversion Principle<br />A. High-level modules should not depend on low-level modules. Both should depend on abstractions.<br />B. Abstractions should not depend upon details. Details should depend upon abstractions.<br />Coined by Martin Fowler<br />
  18. 18. Methods of DI<br />Constructor Injection<br />Setter Injection<br />Interface Injection<br />
  19. 19. IoC Containers<br />How does an IoC container works?<br />
  20. 20. Popular IoC containers for Java<br />Pico Container<br />Spring<br />JBoss Micro kernel<br />Google Guice<br />OSGi ( sort of DI, but very advanced)<br />
  21. 21. Joe managed to save his head!<br />Joe used Spring container.<br />He implemented the MovieLister as a Spring bean.<br />In the beans.xml file, he wired the specific Finder implementation to it’s interface!<br />Voila!<br />
  22. 22. Thank you!<br />We’ll meet in the next week with more Spring examples!<br />Adios!<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×