Dependency Inversion PrincipleBy DunithDhanushka
AgendaWalkthrough of a real world scenario.What is Inversion of Control?What is Dependency Injection?Multiple forms of Dependency InjectionIoC ContainersR.I.P Steve Jobs (1955 -2011)!
SOLID PrinciplesSingle Responsibility PrincipleOpen Closed PrincipleLizkov’s Substitution PrincipleInterface Segregation PrincipleDependency Inversion Principle
Meet Joe the developer!!
Joe’s contract is to build a IMDB clone!
First cut of the Movie Lister app
Problems with itCan’t extend the application without modifying the existing code.Violates Open/Closed principle!!! :S
Introduction of Interfaces
New Movie Lister App
Issues of new Movie ListerMovieLister class has to talk to both interface and implementation!!This makes MovieLister less portable.This makes MovieLister less reusable.Switching implementations requires a code change! :@Hard to unit test.
How can we fix this???
Its IT! We got remedy! ;)Using Setters ( Use a setter to set a Finder instance for MovieLister)Passing a Finder instance as constructor argument for MovieListerUse a Factory of Finders for MovieListerUse a ServiceLocator
Using a setter
What really happened there?MovieLister depends on the FinderPreviously, MovieLister took the control of initiating it’s dependency.But in new version, dependency has been instantiated by some other party for the MovieLister.This is called “Inversion of Control” (IoC)
This makes MovieListerShare across other developers.Makes portable.Encourages unit testing.Preserves OCP (Movie Lister doesn’t know any thing about its crappy implementers. Also plugging a new Finder doesn’t require a code change)Enables the use of Mock Objects.
Ways to achieve IoCDependency InjectionDependency Inversion PrincipleA. High-level modules should not depend on low-level modules. Both should depend on abstractions.B. Abstractions should not depend upon details. Details should depend upon abstractions.Coined by Martin Fowler
Methods of DIConstructor InjectionSetter InjectionInterface Injection
IoC ContainersHow does an IoC container works?
Popular IoC containers for JavaPico ContainerSpringJBoss Micro kernelGoogle GuiceOSGi ( sort of DI, but very advanced)
Joe managed to save his head!Joe used Spring container.He implemented the MovieLister as a Spring bean.In the beans.xml file, he wired the specific Finder implementation to it’s interface!Voila!
Thank you!We’ll meet in the next week with more Spring examples!Adios!

An Introduction to Dependency Inversion Principle

  • 1.
  • 2.
    AgendaWalkthrough of areal world scenario.What is Inversion of Control?What is Dependency Injection?Multiple forms of Dependency InjectionIoC ContainersR.I.P Steve Jobs (1955 -2011)!
  • 3.
    SOLID PrinciplesSingle ResponsibilityPrincipleOpen Closed PrincipleLizkov’s Substitution PrincipleInterface Segregation PrincipleDependency Inversion Principle
  • 4.
    Meet Joe thedeveloper!!
  • 5.
    Joe’s contract isto build a IMDB clone!
  • 6.
    First cut ofthe Movie Lister app
  • 7.
    Problems with itCan’textend the application without modifying the existing code.Violates Open/Closed principle!!! :S
  • 8.
  • 10.
  • 11.
    Issues of newMovie ListerMovieLister class has to talk to both interface and implementation!!This makes MovieLister less portable.This makes MovieLister less reusable.Switching implementations requires a code change! :@Hard to unit test.
  • 12.
    How can wefix this???
  • 13.
    Its IT! Wegot remedy! ;)Using Setters ( Use a setter to set a Finder instance for MovieLister)Passing a Finder instance as constructor argument for MovieListerUse a Factory of Finders for MovieListerUse a ServiceLocator
  • 14.
  • 15.
    What really happenedthere?MovieLister depends on the FinderPreviously, MovieLister took the control of initiating it’s dependency.But in new version, dependency has been instantiated by some other party for the MovieLister.This is called “Inversion of Control” (IoC)
  • 16.
    This makes MovieListerShareacross other developers.Makes portable.Encourages unit testing.Preserves OCP (Movie Lister doesn’t know any thing about its crappy implementers. Also plugging a new Finder doesn’t require a code change)Enables the use of Mock Objects.
  • 17.
    Ways to achieveIoCDependency InjectionDependency Inversion PrincipleA. High-level modules should not depend on low-level modules. Both should depend on abstractions.B. Abstractions should not depend upon details. Details should depend upon abstractions.Coined by Martin Fowler
  • 18.
    Methods of DIConstructorInjectionSetter InjectionInterface Injection
  • 19.
    IoC ContainersHow doesan IoC container works?
  • 20.
    Popular IoC containersfor JavaPico ContainerSpringJBoss Micro kernelGoogle GuiceOSGi ( sort of DI, but very advanced)
  • 21.
    Joe managed tosave his head!Joe used Spring container.He implemented the MovieLister as a Spring bean.In the beans.xml file, he wired the specific Finder implementation to it’s interface!Voila!
  • 22.
    Thank you!We’ll meetin the next week with more Spring examples!Adios!