Object-oriented – Dependency Inversion Principle: How to define “high-level policy” and “low-level detail” to other people

designdesign-patternsobject-orientedsolid

I am trying to explain the dependency inversion principle to my (mostly junior) colleagues. How can we define which one is the "high-level policy" and which one is the "low-level detail" in a software? For example, if our software automates workflow of several business applications, why do we say that the workflow automation is the high-level policy and the business applications are the details?

Best Answer

Note: this has been completely rewritten from my earlier example

Think about power sockets. In any given nation, the high-level policy is that power sockets are always the same. It doesn't matter where you get you electricity from (coal, gas, nuclear), the sockets on the wall should always put out the same amount of power, through the same set of connectors.

Now you can plug any device into that socket, because they all have a common interface, the "plug". The high-level policy never has to dictate any part of that implementation detail. Just plug something in and it goes.

Now if you have a device that doesn't want AC power -- maybe it runs on a 7V DC circuit -- you can still use that high-level policy, you just need some kind of adapter between the power supply and the device. And, because everyone has the same high-level policy, the manufacturer can build that into the implementation, without any change to the high-level policy. The person connecting the implementation to the policy (you, plugging your laptop in) doesn't really need to understand either.

Further, if the manufacturer wants to sell the same device in another country, all they have to do is develop a different adapter. So the same implementation can work with multiple policies while the same policy can run multiple implementations.

This is a perfect example of dependency inversion.

But here's the interesting bit: Go back to what I first said. "It doesn't matter where you get you electricity from." This is also an implementation detail. The high-level policy is still that all power sockets are the same shape and emit the same type of power. The low-level implementation details are both where the electricity comes from and what it runs.

In programming terms, that means the high-level policy is the interface (Where a language supports it. Another form of DI is duck-typing.) that an API provides and the application consumes, and the low-level implementation details are both the application consuming it and the API itself, neither of which need to understand each other.

Adapters may be used to fit the same implementation into different policies.