SlideShare a Scribd company logo
1 of 22
Dependency Inversion Principle By DunithDhanushka
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)!
SOLID Principles Single Responsibility Principle Open Closed Principle Lizkov’s Substitution Principle Interface Segregation Principle Dependency Inversion Principle
Meet Joe the developer!!
Joe’s contract is to build a IMDB clone!
First cut of the Movie Lister app
Problems with it Can’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 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.
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 MovieLister Use a Factory of Finders for MovieLister Use a ServiceLocator
Using a setter
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)
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.
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
Methods of DI Constructor Injection Setter Injection Interface Injection
IoC Containers How does an IoC container works?
Popular IoC containers for Java Pico Container Spring JBoss Micro kernel Google Guice OSGi ( 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!

More Related Content

What's hot

Getting started with dagger 2
Getting started with dagger 2Getting started with dagger 2
Getting started with dagger 2Rodrigo Henriques
 
Testing the untestable
Testing the untestableTesting the untestable
Testing the untestableRoyKlein
 
10 Usability Heuristics - IntelliJ IDEA
10 Usability Heuristics - IntelliJ IDEA10 Usability Heuristics - IntelliJ IDEA
10 Usability Heuristics - IntelliJ IDEANirodha Perera
 
Moderne App-Architektur mit Dagger2 und RxJava
Moderne App-Architektur mit Dagger2 und RxJavaModerne App-Architektur mit Dagger2 und RxJava
Moderne App-Architektur mit Dagger2 und RxJavainovex GmbH
 
20080531 Intro To Dependency Injection & Inversion Of Control
20080531 Intro To Dependency Injection & Inversion Of Control20080531 Intro To Dependency Injection & Inversion Of Control
20080531 Intro To Dependency Injection & Inversion Of Controldonnfelker
 
Bartlesville Dot Net User Group Design Patterns
Bartlesville Dot Net User Group Design PatternsBartlesville Dot Net User Group Design Patterns
Bartlesville Dot Net User Group Design PatternsJason Townsend, MBA
 
Cool .NET tools, techniques and libraries
Cool .NET tools, techniques and librariesCool .NET tools, techniques and libraries
Cool .NET tools, techniques and librariesGreg Sohl
 
A journey with Target Platforms
A journey with Target PlatformsA journey with Target Platforms
A journey with Target PlatformsMickael Istria
 
Menggunakan NoSQL Database di Aplikasi Android - Ahmad Imaduddin (Google Asso...
Menggunakan NoSQL Database di Aplikasi Android - Ahmad Imaduddin (Google Asso...Menggunakan NoSQL Database di Aplikasi Android - Ahmad Imaduddin (Google Asso...
Menggunakan NoSQL Database di Aplikasi Android - Ahmad Imaduddin (Google Asso...DicodingEvent
 
OS Talks - Documenting Your Existing APIs: API Documentation Made Easy with S...
OS Talks - Documenting Your Existing APIs: API Documentation Made Easy with S...OS Talks - Documenting Your Existing APIs: API Documentation Made Easy with S...
OS Talks - Documenting Your Existing APIs: API Documentation Made Easy with S...Wiratama Paramasatya
 
100+ Core java interview questions and Answers
100+ Core java interview questions and Answers100+ Core java interview questions and Answers
100+ Core java interview questions and AnswersTutorials Mate
 
Visual Studio 2015 Diagnostic and Debugging Tools
Visual Studio 2015 Diagnostic and Debugging ToolsVisual Studio 2015 Diagnostic and Debugging Tools
Visual Studio 2015 Diagnostic and Debugging ToolsMikhail Shcherbakov
 
Continuous Integration in Action
Continuous Integration in ActionContinuous Integration in Action
Continuous Integration in ActionBoris Modylevsky
 

What's hot (20)

Getting started with dagger 2
Getting started with dagger 2Getting started with dagger 2
Getting started with dagger 2
 
Testing the untestable
Testing the untestableTesting the untestable
Testing the untestable
 
PhD Proposal
PhD ProposalPhD Proposal
PhD Proposal
 
10 Usability Heuristics - IntelliJ IDEA
10 Usability Heuristics - IntelliJ IDEA10 Usability Heuristics - IntelliJ IDEA
10 Usability Heuristics - IntelliJ IDEA
 
Choosing an IoC container
Choosing an IoC containerChoosing an IoC container
Choosing an IoC container
 
Unit testing with Jasmine
Unit testing with JasmineUnit testing with Jasmine
Unit testing with Jasmine
 
Code Review
Code ReviewCode Review
Code Review
 
Design Smells
Design SmellsDesign Smells
Design Smells
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Moderne App-Architektur mit Dagger2 und RxJava
Moderne App-Architektur mit Dagger2 und RxJavaModerne App-Architektur mit Dagger2 und RxJava
Moderne App-Architektur mit Dagger2 und RxJava
 
20080531 Intro To Dependency Injection & Inversion Of Control
20080531 Intro To Dependency Injection & Inversion Of Control20080531 Intro To Dependency Injection & Inversion Of Control
20080531 Intro To Dependency Injection & Inversion Of Control
 
Bartlesville Dot Net User Group Design Patterns
Bartlesville Dot Net User Group Design PatternsBartlesville Dot Net User Group Design Patterns
Bartlesville Dot Net User Group Design Patterns
 
Cool .NET tools, techniques and libraries
Cool .NET tools, techniques and librariesCool .NET tools, techniques and libraries
Cool .NET tools, techniques and libraries
 
A journey with Target Platforms
A journey with Target PlatformsA journey with Target Platforms
A journey with Target Platforms
 
Menggunakan NoSQL Database di Aplikasi Android - Ahmad Imaduddin (Google Asso...
Menggunakan NoSQL Database di Aplikasi Android - Ahmad Imaduddin (Google Asso...Menggunakan NoSQL Database di Aplikasi Android - Ahmad Imaduddin (Google Asso...
Menggunakan NoSQL Database di Aplikasi Android - Ahmad Imaduddin (Google Asso...
 
OS Talks - Documenting Your Existing APIs: API Documentation Made Easy with S...
OS Talks - Documenting Your Existing APIs: API Documentation Made Easy with S...OS Talks - Documenting Your Existing APIs: API Documentation Made Easy with S...
OS Talks - Documenting Your Existing APIs: API Documentation Made Easy with S...
 
100+ Core java interview questions and Answers
100+ Core java interview questions and Answers100+ Core java interview questions and Answers
100+ Core java interview questions and Answers
 
Visual Studio 2015 Diagnostic and Debugging Tools
Visual Studio 2015 Diagnostic and Debugging ToolsVisual Studio 2015 Diagnostic and Debugging Tools
Visual Studio 2015 Diagnostic and Debugging Tools
 
Continuous Integration in Action
Continuous Integration in ActionContinuous Integration in Action
Continuous Integration in Action
 

Similar to An Introduction to Dependency Inversion Principle

Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy
 
Introduction To Dependency Injection Using Spring.NET
Introduction To Dependency Injection Using Spring.NETIntroduction To Dependency Injection Using Spring.NET
Introduction To Dependency Injection Using Spring.NETRyan Montgomery
 
Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)Simone Chiaretta
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofacmeghantaylor
 
An introduction to the Spring Framework
An introduction to the Spring FrameworkAn introduction to the Spring Framework
An introduction to the Spring Frameworkweili_at_slideshare
 
Odo improving the developer experience on OpenShift - hack & sangria
Odo   improving the developer experience on OpenShift - hack & sangriaOdo   improving the developer experience on OpenShift - hack & sangria
Odo improving the developer experience on OpenShift - hack & sangriaJorge Morales
 
Brownbag001 spring ioc from 2012
Brownbag001 spring ioc from 2012Brownbag001 spring ioc from 2012
Brownbag001 spring ioc from 2012Timothy Spann
 
What is Codename One - Transcript.pdf
What is Codename One - Transcript.pdfWhat is Codename One - Transcript.pdf
What is Codename One - Transcript.pdfShaiAlmog1
 
springtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdfspringtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdfBruceLee275640
 
Brief introduction into dependencies
Brief introduction into dependenciesBrief introduction into dependencies
Brief introduction into dependenciesJoel Krebs
 
P2 Introduction
P2 IntroductionP2 Introduction
P2 Introductionirbull
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring FrameworkASG
 
Appium overview (Selenium Israel #2, Feb. 2014)
Appium overview (Selenium Israel #2, Feb. 2014)Appium overview (Selenium Israel #2, Feb. 2014)
Appium overview (Selenium Israel #2, Feb. 2014)danielputerman
 
EkoParty 2010: iPhone Rootkit? There's an App for that.
EkoParty 2010: iPhone Rootkit? There's an App for that.EkoParty 2010: iPhone Rootkit? There's an App for that.
EkoParty 2010: iPhone Rootkit? There's an App for that.Eric Monti
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applicationsBabak Naffas
 
ISS Art. How to do IT. Kotlin Multiplatform
ISS Art. How to do IT. Kotlin MultiplatformISS Art. How to do IT. Kotlin Multiplatform
ISS Art. How to do IT. Kotlin MultiplatformISS Art, LLC
 
Introduction to dependency injection in Scala (Play)
Introduction to dependency injection in Scala (Play)Introduction to dependency injection in Scala (Play)
Introduction to dependency injection in Scala (Play)Knoldus Inc.
 
RubyMotion Inspect Conference - 2013. (Without speaker notes.)
RubyMotion Inspect Conference - 2013. (Without speaker notes.)RubyMotion Inspect Conference - 2013. (Without speaker notes.)
RubyMotion Inspect Conference - 2013. (Without speaker notes.)alloy020
 
GITS Class #20: Building A Fast and Responsive UI in React Native
GITS Class #20: Building A Fast and Responsive UI in React NativeGITS Class #20: Building A Fast and Responsive UI in React Native
GITS Class #20: Building A Fast and Responsive UI in React NativeGITS Indonesia
 

Similar to An Introduction to Dependency Inversion Principle (20)

Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
 
Introduction To Dependency Injection Using Spring.NET
Introduction To Dependency Injection Using Spring.NETIntroduction To Dependency Injection Using Spring.NET
Introduction To Dependency Injection Using Spring.NET
 
Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofac
 
Mocast Postmortem
Mocast PostmortemMocast Postmortem
Mocast Postmortem
 
An introduction to the Spring Framework
An introduction to the Spring FrameworkAn introduction to the Spring Framework
An introduction to the Spring Framework
 
Odo improving the developer experience on OpenShift - hack & sangria
Odo   improving the developer experience on OpenShift - hack & sangriaOdo   improving the developer experience on OpenShift - hack & sangria
Odo improving the developer experience on OpenShift - hack & sangria
 
Brownbag001 spring ioc from 2012
Brownbag001 spring ioc from 2012Brownbag001 spring ioc from 2012
Brownbag001 spring ioc from 2012
 
What is Codename One - Transcript.pdf
What is Codename One - Transcript.pdfWhat is Codename One - Transcript.pdf
What is Codename One - Transcript.pdf
 
springtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdfspringtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdf
 
Brief introduction into dependencies
Brief introduction into dependenciesBrief introduction into dependencies
Brief introduction into dependencies
 
P2 Introduction
P2 IntroductionP2 Introduction
P2 Introduction
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Appium overview (Selenium Israel #2, Feb. 2014)
Appium overview (Selenium Israel #2, Feb. 2014)Appium overview (Selenium Israel #2, Feb. 2014)
Appium overview (Selenium Israel #2, Feb. 2014)
 
EkoParty 2010: iPhone Rootkit? There's an App for that.
EkoParty 2010: iPhone Rootkit? There's an App for that.EkoParty 2010: iPhone Rootkit? There's an App for that.
EkoParty 2010: iPhone Rootkit? There's an App for that.
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applications
 
ISS Art. How to do IT. Kotlin Multiplatform
ISS Art. How to do IT. Kotlin MultiplatformISS Art. How to do IT. Kotlin Multiplatform
ISS Art. How to do IT. Kotlin Multiplatform
 
Introduction to dependency injection in Scala (Play)
Introduction to dependency injection in Scala (Play)Introduction to dependency injection in Scala (Play)
Introduction to dependency injection in Scala (Play)
 
RubyMotion Inspect Conference - 2013. (Without speaker notes.)
RubyMotion Inspect Conference - 2013. (Without speaker notes.)RubyMotion Inspect Conference - 2013. (Without speaker notes.)
RubyMotion Inspect Conference - 2013. (Without speaker notes.)
 
GITS Class #20: Building A Fast and Responsive UI in React Native
GITS Class #20: Building A Fast and Responsive UI in React NativeGITS Class #20: Building A Fast and Responsive UI in React Native
GITS Class #20: Building A Fast and Responsive UI in React Native
 

Recently uploaded

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 

Recently uploaded (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

An Introduction to Dependency Inversion Principle

  • 1. Dependency Inversion Principle By DunithDhanushka
  • 2. 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)!
  • 3. SOLID Principles Single Responsibility Principle Open Closed Principle Lizkov’s Substitution Principle Interface Segregation Principle Dependency Inversion Principle
  • 4. Meet Joe the developer!!
  • 5. Joe’s contract is to build a IMDB clone!
  • 6. First cut of the Movie Lister app
  • 7. Problems with it Can’t extend the application without modifying the existing code. Violates Open/Closed principle!!! :S
  • 9.
  • 11. 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.
  • 12. How can we fix this???
  • 13. 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
  • 15. 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)
  • 16. 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.
  • 17. 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
  • 18. Methods of DI Constructor Injection Setter Injection Interface Injection
  • 19. IoC Containers How does an IoC container works?
  • 20. Popular IoC containers for Java Pico Container Spring JBoss Micro kernel Google Guice OSGi ( sort of DI, but very advanced)
  • 21. 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!
  • 22. Thank you! We’ll meet in the next week with more Spring examples! Adios!