Dependency Inversion Principle

DIP:

Depend upon abstraction. Code to interfaces and not to concrete implementations.

This is similar to the OO principle: "program to an interface and not to an implementation". However, the DIP makes an even stronger statement about abstraction. It means any component (high or low level) within an application should depend upon abstractions. A high level component is one whose behavior is defined in terms of other lower level components.

The following guidelines help in avoiding OO designs that violate the DIP:

  1. No variable should hold a reference to a concrete class. Do not use new, use factories.
  2. Do not derive from concrete classes. Derive from abstract or interfaces. If your are deriving from concrete classes then there is a dependence on the concrete class.
  3. Do not override base class methods. Those concrete methods in the base class are meant to be shared between all the derived classes.

Inverson of control:

This generic term is also synonymous with Dependency Injection.

The assembler injects the dependency and reduces tight coupling between classes.

Leave a Reply

Your email address will not be published. Required fields are marked *

     

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">