Download presentation
Presentation is loading. Please wait.
Published byClifford Hicks Modified over 9 years ago
1
Dependency Inversion Principle Jon McBee Principal Software Engineer Ultratech CNT
2
Dependency Inversion Principle Depend upon abstract entities, not concrete entities
3
A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend on details. Details should depend on abstractions.
4
A. High-level modules should not depend on low-level modules. Both should depend on abstractions. Message Broker Message Transport QueueNotifierAF Message
5
B. Abstractions should not depend on details. Details should depend on abstractions.
6
By passing dependencies to classes as abstractions, you remove the need to program dependency specific By giving a craftsman a Craftsman Bolt On® base, you remove the need for the craftsman to lug around a bag of tools
7
B. Abstractions should not depend on details. Details should depend on abstractions. Message Broker Message Transport QueueNotifierAF MessageEvent Adding an Event Message Transport type should not Cause the Message Transport interface to change
8
B. Abstractions should not depend on details. Details should depend on abstractions. Message Broker Message Transport Event
9
Dependency Inversion Principle
10
Two Types of Dependency 1.Runtime dependency exists whenever two modules interact at runtime 2.Source code dependency exists when a method defined by one module is called by another module
11
Runtime Dependency Tree Black Hole Travelers Guide Calculate Time Dilation Calculate Tidal Forces Calculate Evaporation Rate Calculate Mass Calculate Event Horizon
12
Source Code Dependency Tree Black Hole Travelers Guide Calculate Time Dilation Calculate Tidal Forces Calculate Evaporation Rate Calculate Mass Calculate Event Horizon
13
Break the Source Code Dependency Calculate Tidal Forces Calculate Mass + Calculate Mass
14
Break the Source Code Dependency Calculate Tidal Forces Abstract Calculate Mass + Calculate Mass Calculate Mass + Calculate Mass http://www.labviewcraftsmen.com/blog/fun-with-the-actor-framework-low-coupled-messaging
15
Dependency Inversion Calculate Tidal Forces Abstract Calculate Mass + Calculate Mass Calculate Mass + Calculate Mass Calculate Tidal Forces Calculate Mass + Calculate Mass Source Code Dependency TreeRuntime Dependency Tree “Dependencies are inverted whenever the source code dependencies oppose the direction of the flow of control” -Bob Martin
16
Discussion Question Q: Assume that we are writing an application for our HR department that tracks employees and that we have an Employee class that needs to be able to save data to both XML and to a database. How should we give the Employee class this ability? P1: Should we give the Employee class ToXML.vi and ToDB.vi methods? P2: Should we give the Employee class a Write.vi method that takes as an input a flag for either writing to XML or to DB using an XML or DB object respectively?
17
Discussion Question Q: Assume that we have are writing an application for our HR department that tracks employees and that we have an Employee class that needs to be able to save data to XML and to a database. How should we give the Employee class this ability? P1: Should we give the Employee class ToXML.vi and ToDB.vi methods? This would violate SRP! P2: Should we give the Employee class a Write.vi method that takes as an input a flag for either writing to XML or to DB using an XML or DB object respectively? This would violate OCP!
18
Discussion Question Q: Assume that we have are writing an application for our HR department that tracks employees and that we have an Employee class that needs to be able to save data to XML and to a database. How should we give the Employee class this ability? EmployeeiDataWriter + Write XML + Write DB + Write
19
Dependency Injection Dependency Injection is NOT Dependency Inversion! You must first invert your dependencies to properly apply dependency injection Dependency Inversion Dependency Injection
20
Demo: DIP in the Wild
21
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.