SlideShare ist ein Scribd-Unternehmen logo
1 von 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!

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie Dependency Inversion Principle explained with real-world example

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
 

Ähnlich wie Dependency Inversion Principle explained with real-world example (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
 

Kürzlich hochgeladen

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 

Kürzlich hochgeladen (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 

Dependency Inversion Principle explained with real-world example

  • 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!