An Introduction to Dependency Inversion Principle

22
Dependency Inversion Principle By Dunith Dhanushka

description

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

Transcript of An Introduction to Dependency Inversion Principle

Page 1: An Introduction to Dependency Inversion Principle

Dependency Inversion PrincipleBy Dunith Dhanushka

Page 2: An Introduction to Dependency Inversion Principle

Agenda

• Walkthrough of a real world scenario.• What is Inversion of Control?• What is Dependency Injection?• Multiple forms of Dependency Injection• IoC Containers

R.I.P Steve Jobs (1955 -2011)!

Page 3: An Introduction to Dependency Inversion Principle

SOLID Principles

• Single Responsibility Principle• Open Closed Principle• Lizkov’s Substitution Principle• Interface Segregation Principle

• Dependency Inversion Principle

Page 4: An Introduction to Dependency Inversion Principle

Meet Joe the developer!!

Page 5: An Introduction to Dependency Inversion Principle

Joe’s contract is to build a IMDB clone!

Page 6: An Introduction to Dependency Inversion Principle

First cut of the Movie Lister app

Page 7: An Introduction to Dependency Inversion Principle

Problems with it

• Can’t extend the application without modifying the existing code.

• Violates Open/Closed principle!!! :S

Page 8: An Introduction to Dependency Inversion Principle

Introduction of Interfaces

Page 9: An Introduction to Dependency Inversion Principle
Page 10: An Introduction to Dependency Inversion Principle

New Movie Lister App

Page 11: An Introduction to Dependency Inversion Principle

Issues of new Movie Lister

• MovieLister 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.

Page 12: An Introduction to Dependency Inversion Principle

How can we fix this???

Page 13: An Introduction to Dependency Inversion Principle

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 MovieLister

• Use a Factory of Finders for MovieLister • Use a ServiceLocator

Page 14: An Introduction to Dependency Inversion Principle

Using a setter

Page 15: An Introduction to Dependency Inversion Principle

What really happened there?

• MovieLister depends on the Finder• Previously, 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)

Page 16: An Introduction to Dependency Inversion Principle

This makes MovieLister

• Share 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.

Page 17: An Introduction to Dependency Inversion Principle

Ways to achieve IoC

• Dependency Injection• Dependency Inversion Principle– A. 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

Page 18: An Introduction to Dependency Inversion Principle

Methods of DI

• Constructor Injection• Setter Injection• Interface Injection

Page 19: An Introduction to Dependency Inversion Principle

IoC Containers

• How does an IoC container works?

Page 20: An Introduction to Dependency Inversion Principle

Popular IoC containers for Java

• Pico Container• Spring• JBoss Micro kernel• Google Guice• OSGi ( sort of DI, but very advanced)

Page 21: An Introduction to Dependency Inversion Principle

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!

Page 22: An Introduction to Dependency Inversion Principle

Thank you!

• We’ll meet in the next week with more Spring examples!

• Adios!