Dependency Inversion Principle | SOLID as a Rock
Tags: cpp, codenewbie, tutorial, design
Published: 2020-06-06 10:02:29

**D**ependency **I**nversion **P**rinciple(in C++) is the fifth & last design principle of a series SOLID as a Rock design principles. The SOLID design principles focus on developing software that is easy to maintainable, reusable & extendable. In this article, we will see an example code with the flaw & correct it with help of DIP. We will also see guideline & benefits of DIP in closure of the article.

> /!\\\: This article has been originally published on my [blog](http://www.vishalchovatiya.com/dependency-inversion-principle-in-cpp-solid-as-a-rock/). If you are interested in receiving my latest articles, [please sign up to my newsletter](http://eepurl.com/gDNybv).

By the way, If you haven't gone through my previous articles on design principles, then below is the quick links:

1.  [**S**RP -- Single Responsibility Principle](http://www.vishalchovatiya.com/single-responsibility-principle-in-cpp-solid-as-a-rock/)
2.  [**O**CP -- Open/Closed Principle](http://www.vishalchovatiya.com/open-closed-principle-in-cpp-solid-as-a-rock/)
3.  [**L**SP -- Liskov Substitution Principle](http://www.vishalchovatiya.com/liskovs-substitution-principle-in-cpp-solid-as-a-rock/)
4.  [**I**SP -- Interface Segregation Principle](http://www.vishalchovatiya.com/interface-segregation-principle-in-cpp-solid-as-a-rock/)
5.  [**D**IP -- Dependency Inversion Principle](http://www.vishalchovatiya.com/dependency-inversion-principle-in-cpp-solid-as-a-rock/)

The code snippets you see throughout this series of articles are simplified not sophisticated. So you often see me not using keywords like `override`, `final`, `public`(while inheritance) just to make code compact & consumable(most of the time) in single standard screen size. I also prefer `struct` instead of `class` just to save line by not writing "`public:`" sometimes and also miss [virtual destructor](http://www.vishalchovatiya.com/part-3-all-about-virtual-keyword-in-c-how-virtual-destructor-works/), constructor, [copy constructor](http://www.vishalchovatiya.com/all-about-copy-constructor-in-cpp-with-example/), prefix `std::`, deleting dynamic memory, intentionally. I also consider myself a pragmatic person who wants to convey an idea in the simplest way possible rather than the standard way or using Jargons.

***Note:***

-   If you stumbled here directly, then I would suggest you go through [What is design pattern?](http://www.vishalchovatiya.com/what-is-design-pattern/) first, even if it is trivial. I believe it will encourage you to explore more on this topic.
-   All of this code you encounter in this series of articles are compiled using C++20(though I have used [Modern C++](http://www.vishalchovatiya.com/21-new-features-of-modern-cpp-to-use-in-your-project/) features up to C++17 in most cases). So if you don't have access to the latest compiler you can use <https://wandbox.org/> which has preinstalled boost library as well.

Intent
------
> => High-level modules should not depend on low-level modules. Both should depend on abstractions.
=> Abstractions should not depend on details. Details should depend on abstractions.

-   Above lines might seem cryptic at first but don't stick here keep going. You will get it by example.

**What are the High-level & Low-level modules?**

***=>*** **High-level modules**: *describes operations* which is more abstract in nature & contain more complex logic. These modules orchestrate low-level modules in our application.
***=>*** **Low-level modules**: *describes implementations* more specific & individual to components focusing on details & smaller parts of the application. These modules are used inside the high-level modules.

Violating Dependency Inversion Principle
----------------------------------------------------
```c++
enum class Relationship { parent, child, sibling };

struct Person {
    string      m_name;
};

struct Relationships {      // Low-level <<<<<<<<<<<<-------------------------
    vector<tuple<Person, Relationship, Person>>     m_relations;

    void add_parent_and_child(const Person &parent, const Person &child) {
        m_relations.push_back({parent, Relationship::parent, child});
        m_relations.push_back({child, Relationship::child, parent});
    }
};

struct Research {           // High-level  <<<<<<<<<<<<------------------------
    Research(const Relationships &relationships) {
        for (auto &&[first, rel, second] : relationships.m_relations) {// Need C++17 here
            if (first.m_name == "John" && rel == Relationship::parent)
                cout << "John has a child called " << second.m_name << endl;
        }
    }
};

int main() {
    Person parent{"John"};
    Person child1{"Chris"};
    Person child2{"Matt"};

    Relationships relationships;
    relationships.add_parent_and_child(parent, child1);
    relationships.add_parent_and_child(parent, child2);

    Research _(relationships);

    return EXIT_SUCCESS;
}
```
-   When later on the container of `Relationships` changes from `vector` to `set` or any other container, you need to change in many places which isn't a very good design. Even if just the name of data member i.e. `Relationships::m_relations` changes, you will find yourself breaking other parts of code.
-   As you can see Low-level module i.e. `Relationships` directly depend on High-level module i.e. `Research` which is essentially a violation of DIP.

Dependency Inversion Principle Example
----------------------------------------------------------

-   Rather we should create an abstraction and bind Low-level & High-level module to that abstraction. Consider the following fix:

```c++
struct RelationshipBrowser {
    virtual vector<Person> find_all_children_of(const string &name) = 0;
};

struct Relationships : RelationshipBrowser {     // Low-level <<<<<<<<<<<<<<<------------------------
    vector<tuple<Person, Relationship, Person>>     m_relations;

    void add_parent_and_child(const Person &parent, const Person &child) {
        m_relations.push_back({parent, Relationship::parent, child});
        m_relations.push_back({child, Relationship::child, parent});
    }

    vector<Person> find_all_children_of(const string &name) {
        vector<Person> result;
        for (auto &&[first, rel, second] : m_relations) {
            if (first.name == name && rel == Relationship::parent) {
                result.push_back(second);
            }
        }
        return result;
    }
};

struct Research {                                // High-level <<<<<<<<<<<<<<<----------------------
    Research(RelationshipBrowser &browser) {
        for (auto &child : browser.find_all_children_of("John")) {
            cout << "John has a child called " << child.name << endl;
        }
    }
    //  Research(const Relationships& relationships)
    //  {
    //    auto& relations = relationships.relations;
    //    for (auto&& [first, rel, second] : relations)
    //    {
    //      if (first.name == "John" && rel == Relationship::parent)
    //      {
    //        cout << "John has a child called " << second.name << endl;
    //      }
    //    }
    //  }
};
```
-   Now no matter, the name of container or container itself changes in Low-level module, High-level module or other parts of code which followed DIP will be in-tact.
-   The Dependency Inversion Principle (DIP) suggest that the most flexible systems are those in which source code dependencies refer only to abstractions, not to concretions.
-   This is the reason why most experienced dev uses STL or library functions along with generic containers. Even using an `auto` keyword at appropriate places may help in creating generic behaviour with less fragile code.
-   There are many ways you can implement DIP, as long as C++ concerns most people use static polymorphism(i.e. [CRTP](http://www.vishalchovatiya.com/7-advanced-cpp-concepts-idiom-examples-you-should-know/#CRTP) unless they need dynamic one), [template specialization](https://stackoverflow.com/a/43576623), [Adapter Design Pattern](http://www.vishalchovatiya.com/adapter-design-pattern-in-modern-cpp/), [type-erasure](http://www.vishalchovatiya.com/7-advanced-cpp-concepts-idiom-examples-you-should-know/#Type-Eraser), etc.

Yardstick to Craft Dependency Inversion Principle(DIP) Friendly Software
-------------------------------------------------------------------------------

-   If you find enforcing DIP difficult then just design abstraction first & implement your high-level module on the bases of abstraction. Without having any knowledge of the low-level module or its implementation. Because of this process DIP is also known as ***Coding To Interface***.
-   Keep in mind that all Low-level-modules/[subclasses](http://www.vishalchovatiya.com/memory-layout-of-cpp-object/) adhere to the [Liskov Substitution Principle](http://www.vishalchovatiya.com/liskovs-substitution-principle-in-cpp-solid-as-a-rock/). This is because the Low-level-modules/[subclasses](http://www.vishalchovatiya.com/inside-the-cpp-object-model/) will be used via the abstract interface, not the concrete classes interface.

Benefits
--------

### => Reusability

-   Effectively, the DIP reduces coupling between different pieces of code. Thus we get reusable code.

### => Maintainability

-   It is also important to mention that changing already implemented modules is risky. By depending on abstraction & not on concrete implementation, we can reduce that risk by not having to change high-level modules in our project.
-   Finally, DIP when applied correctly gives us flexibility and stability at the level of the entire architecture of our application. Our application will be able to evolve more securely and become stable & robust.

Conclusion
----------

As you can see we took a basic example of code & converted it into a reusable, flexible & modular piece of code. If I would have to summarize DIP in simple & short sentence then it would be like:

> Do not use the concrete object directly unless you have a strong reason to do so. Use abstraction instead.

DIP trains us to think about classes in terms of behaviour, rather than construction or implementation.




[Have Any Suggestions, Query or Wants to Say `Hi`? Take the Pressure Off, You Are Just a Click Away.](http://www.vishalchovatiya.com/contact-2/)🖱️