Monday, March 07, 2016

Dependency Inversion Principle

The article

The Three Basic Rules for a Good Design

illustrates the Dependency Inversion Principle in action by using a simple example. It applies three simple rules:

  1. Separate things that change from things that stays the same. Encapsulate what varies behind a well-defined interface.
  2. Program to interfaces, not implementations. This exploits polymorphism.
  3. Depend on abstractions. Do not depend on concrete classes.
to satisfy Dependency Inversion Principle. 

Storage mechanisms such as PersonMemoryStore and PersonFileStore have a well defined interface called records. The mechanisms to send the message such as GreetingConsole and GreetingPony have a well defined interface call send. Classes that implement a specific storage or sending email now conform to a uniform interface. This gives us the ability to switch implementation in different combinations. The classes that implement specific way of doing things depend on stable abstractions, records and send that we came up with in the final design.

No comments:

Post a Comment