Sitemap

SOLID-Dependency Inversion

3 min readJul 13, 2022

--

In our developer careers, we always aim to make our work easier as possible.

Making things simple has a lot of benefits in our work. A wise one once to said:

A good software engineer should live a very simple and boring life in their career, any requirement change or new feature implemented shouldn't be a challenge.
If you find out that you take a lot of energy due to some requirement changed and have a lot of sense of achievement after you finish, you must did something wrong with your architecture.

Ok, obviously he is kidding, there are still a lot of challenges in our life.

But the point that above wants to say is, with good architecture, you should consider this requirement will be changed in the future(almost definitely will change in one day.) and should prepare for any change implementation without any side effects or bugs as possible.

What kind of change may happen? Let’s see an example below:

Example of not using dependency injection.

In the above example, we have FeedViewController and can fetch the feed from the backend by using getFeed() method, soFeedViewController can do something with the feed (maybe render it on screen for the user).

Press enter or click to view image in full size
Diagram of example

But one day, you have been told that now we don’t want to fetch the feed from the backend, we want to fetch it from the local storage instead.

In this case, you might will do something as below example:

If we want to shift the feed resource from the backend to local storage, it will break the code.

Due to the requirement changed, you might need to change the feedService to FeedStorage (and of course, the name offeedService might need to change depending on the developer ).

Press enter or click to view image in full size
Diagram of changing the source of feed.

So it will break the code in lines 15 and 18.

Get Gordon Feng’s stories in your inbox

Join Medium for free to get updates from this writer.

Please note, cause this is just a very simple demonstration, high chance that it will need to make changes that way more than above in a real situation.

Decoupling Provider from FeedViewController

The goal we want to achieve is not to break the existing code and switch the feed provider easily. By not breaking any existing code, we will minimize the number of side effects and bugs.

If we search for dependency injection definition:

Depend upon abstractions, not concretions.

The easy way to decouple is to avoid depending on concretions but depend on the abstractions layer instead, and concretions should depend on the abstractions layer.

What is that mean and how should we do it in our code based on it?

Let’s see below example:

Result:

Feed from backend.
Feed from local storage.

For FeedViewController, it doesn’t matter how the feed gets from, it only matters that FeedViewController will get the feed and do what it suppose to do, the feed can come from anywhere, as long as FeedViewController can get the feed.

So first of all, we change the feedServer to feedProvider, which is actually a protocol- FeedProvider as an interface. In this way, FeedViewController will no longer depend on concrete classes but depend on the abstractions layer (FeedProvider Protocol) instead.

Second, we let the FeedService and FeedLocalStorage conform to the FeedProvider protocol, and implement the detail of how each provides the feed(in lines 8 and 20 by using an extension).

Press enter or click to view image in full size
Diagram of after decoupling.

With these two steps, we can now shift the feed source between feedService and feedLocalStorage easily as lines 51 to 54 show, without any exciting code-breaking.

So this will be the third article of the SOLID principle, you can find the Single Responsibility and Open-Close principle here.

If you like the SOLID principle and think this help, please give me some claps, thanks:)

--

--