Dependency inversion principle

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In object-oriented programming, the dependency inversion principle refers to a specific form of decoupling software modules. When following this principle, the conventional dependency relationships established from high-level, policy-setting modules to low-level, dependency modules are inverted (i.e. reversed), thus rendering high-level modules independent of the low-level module implementation details. The principle states:[1]

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.

The principle inverts the way some people may think about object-oriented design, dictating that both high- and low-level objects must depend on the same abstraction.[2]

Traditional layers pattern[edit]

In conventional application architecture, lower-level components are designed to be consumed by higher-level components which enable increasingly complex systems to be built. In this composition, higher-level components depend directly upon lower-level components to achieve some task. This dependency upon lower-level components limits the reuse opportunities of the higher-level components.[3]

Traditional Layers Pattern.png

The goal of the dependency inversion principle is to avoid this highly coupled distribution with the mediation of an abstract layer, and to increase the re-usability of higher/policy layers.

Ownership inversion[edit]

With the addition of an abstract layer, both high- and lower-level layers avoid the traditional dependencies from top to bottom. Nevertheless the ″inversion″ concept does not mean that lower-level layers depend on higher-level layers. Both layers should depend on abstractions that draw the behavior needed by higher-level layers.


In a direct application of dependency inversion, the abstracts are owned by the upper/policy layers. This architecture groups the higher/policy components and the abstracts that define lower services together in the same package. The lower-level layers are created by inheritance/implementation of these abstract classes or interfaces.[4]

The inversion of the dependencies and ownership encourages the re-usability of the higher/policy layers. Upper layers could use other implementations of the lower services. When the lower-level layer components are closed or when the application requires the reuse of existing services, it is common that an Adapter mediates between the services and the abstractions.

Abstraction dependency[edit]

The presence of abstractions to accomplish DIP have other design implications in an Object Oriented program:

  • All member variables in a class must be interfaces or abstracts.
  • All concrete class packages must connect only through interface/abstract classes packages.
  • No class should derive from a concrete class.
  • No method should override an implemented method.[5]
  • All variable instantiation requires the implementation of a Creational pattern as the Factory Method or the Factory pattern, or the more complex use of a Dependency Injection framework.

DIP implementations[edit]

Two common implementations of DIP use similar logical architecture, with different implications.

A direct implementation packages the policy classes with service abstracts classes in one library. In this implementation high-level components and low-level components are distributed into separate packages/libraries, where the interfaces defining the behavior/services required by the high-level component are owned by, and exist within the high-level component's library. The implementation of the high-level component's interface by the low level component requires that the low-level component package depend upon the high-level component for compilation, thus inverting the conventional dependency relationship.

Dependency inversion.png

Figures 1 and 2 illustrate code with the same functionality, however in Figure 2, an interface has been used to invert the dependency. The direction of dependency can be chosen to maximize policy code reuse, and eliminate cyclic dependencies.

In this version of DIP, the lower layer component's dependency on the interfaces/abstracts in the higher-level layers makes re-utilization of the lower layer components difficult. This implementation instead ″inverts″ the traditional dependency from top-to-bottom to the opposite from bottom-to-top.

A more flexible solution extracts the abstract components into an independent set of packages/libraries:

DIPLayersPattern v2.png

The separation of all layers into their own package encourages re-utilization of any layer, providing robustness and mobility.[6]

Related patterns[edit]

Applying the dependency inversion principle can also be seen as an example of the Adapter pattern, i.e. the high-level class defines its own adapter interface which is the abstraction that the other high-level classes depend on. The adaptee implementation also depends on the adapter interface abstraction (of course, since it implements its interface) while it can be implemented by using code from within its own low-level module. The high-level has no dependency on the low-level module since it only uses the low-level indirectly through the adapter interface by invoking polymorphic methods to the interface which are implemented by the adaptee and its low-level module.

Various patterns such as Plugin, Service Locator, or Dependency Injection are employed to facilitate the run-time provisioning of the chosen low-level component implementation to the high-level component.


The dependency inversion principle was postulated by Robert C. Martin and described in several publications including the paper Object Oriented Design Quality Metrics: an analysis of dependencies,[7] an article appearing in the C++ Report in May 1996 entitled The Dependency Inversion Principle,[8] and the books Agile Software Development, Principles, Patterns, and Practices, and Agile Principles, Patterns, and Practices in C#.

See also[edit]


  1. ^ Martin, Robert C. (2003). Agile Software Development, Principles, Patterns, and Practices. Prentice Hall. p. 127. ISBN 978-0135974445. 
  2. ^ Freeman, Eric; Freeman, Elisabeth; Kathy, Sierra; Bert, Bates (2004). Hendrickson, Mike; Loukides, Mike, eds. "Head First Design Patterns" (paperback) 1. O'REILLY. ISBN 978-0-596-00712-6. Retrieved 2012-06-21. 
  3. ^ Martin, Robert C. Agile Software Development, Principles, Patterns, and Practices. Prentice Hall. p. 128. ISBN 978-0135974445. 
  4. ^ Martin, Robert C. Agile Software Development, Principles, Patterns, and Practices. Prentice Hall. p. 127-128. ISBN 978-0135974445. 
  5. ^ Martin, Robert C. Agile Software Development, Principles, Patterns, and Practices. Prentice Hall. p. 129. ISBN 978-0135974445. 
  6. ^ Martin, Robert C. Agile Software Development, Principles, Patterns, and Practices. Prentice Hall. p. 131. ISBN 978-0135974445. 
  7. ^ Object Oriented Design Quality Metrics: an analysis of dependencies Robert C. Martin, C++ Report, Sept/Oct 1995
  8. ^ The Dependency Inversion Principle, Robert C. Martin, C++ Report, May 1996

External links[edit]