Giter Site home page Giter Site logo

dip's Introduction

Dedendency Inversion Principle

The D in SOLID Principles.

  • Single responsibility principle
  • Open/closed principle
  • Liskov substitution principle
  • Interface segregation principle
  • Dependency inversion principle

Textbook definition

High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.

More readable definition

policy (high level) should not depend on detail (implementation), but detail should depend on policy. The higher-level policy should define an abstraction that it will call out to, where some detail implementation executes the requested action.

(source: https://lostechies.com/derickbailey/2011/09/22/dependency-injection-is-not-the-same-as-the-dependency-inversion-principle/)

Sudo code excercise to get us thinking:

Given I have a lightbulb LB2300 and Switch SW9000 with the following public methods, write the code necessary for the switch to turn the light on and off. (10 mins)

LB2300
  - turnOn
  - turnOff
SW9000
  - isOn
  - press

[Code: v1.0] With this code, what happens if the manufacturer of the LB2300 comes out with a new model? How can we write our switch to prevent this?

[Code: v1.1] By creating an interface for the switchable objects, I've added a layer of abstraction. Is this really DIP?

Remember, high-level modules should not depend on low-level modules. Identify the high level and low level modules. Who owns the abstraction?

[Code: v1.2] Let's move the abstraction to the high-level module. Now, is this an example of DIP?

Each [Code: tag] corresponds to a release tag in git. You can checkout each release top jump directly to that step.

dip's People

Contributors

adandach avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.