Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dependency Inversion Principle

Similar presentations


Presentation on theme: "Dependency Inversion Principle"— Presentation transcript:

1 Dependency Inversion Principle
Jon McBee President Composed Systems

2 Dependency Inversion Principle
Depend upon abstract entities, not concrete entities

3 A. High-level modules should not depend on low-level modules
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
A. High-level modules should not depend on low-level modules. Both should depend on abstractions. Message Broker Message Transport Notifier Queue AF Message

5 B. Abstractions should not depend on details
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
B. Abstractions should not depend on details. Details should depend on abstractions. Message Broker Adding an Event Message Transport type should not Cause the Message Transport interface to change Message Transport Notifier Queue AF Message Event

8 B. Abstractions should not depend on details
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
Runtime dependency exists whenever two modules interact at runtime 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 Event Horizon Calculate Mass

12 Source Code Dependency Tree
Black Hole Travelers Guide Calculate Time Dilation Calculate Tidal Forces Calculate Evaporation Rate Calculate Event Horizon Calculate Mass

13 Calculate Tidal Forces
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

15 Dependency Inversion Source Code Dependency Tree Runtime Dependency Tree Calculate Tidal Forces Abstract Calculate Mass Calculate Tidal Forces “Dependencies are inverted whenever the source code dependencies oppose the direction of the flow of control” -Bob Martin + Calculate Mass Calculate Mass Calculate Mass + Calculate Mass + Calculate Mass

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 This would violate SRP! This would violate OCP!
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? Employee iDataWriter + Write XML DB + Write + Write

19 Dependency Injection Dependency Injection is NOT Dependency Inversion!
You must first invert your dependencies to properly apply dependency injection Dependency Injection Dependency Inversion

20 Demo: DIP in the Wild

21 Questions?


Download ppt "Dependency Inversion Principle"

Similar presentations


Ads by Google