A. High-level modules should not depend on low-level modules. Both
should depend on abstractions.
B. Abstractions should not depend on details. Details should depend on abstractions.
A long time ago, in a galaxy far, far away, there existed two droids: a supervisor droid (model S-100) who was programmed to routinely issue requests to a cleaner droid (model C-100) to clean a room. The cleaner droid was programmed to grab a broom, use its wheels to go to the appropriate location, and begin cleaning the dirty room.
This was all fine and dandy, until the invention of a new cleaning device for certain situations known as a mop.
Since the C-100 was not programmed to use a mop, a new model of cleaning droid had to be invented that could utilize both a broom and a mop, known as C-101.
However, since the S-100 was not programmed to communicate with anything but a C-100, a new model of the supervisor, S-101, also had to be created to account for C-101.
... and this lead to order in the universe...
... until the invention of a new cleaning device known to the mystics as the dust mop.
At this point, the designers were getting fed up. To create a new C-102 model here to handle this new and alien cleaning device would likewise require a new S-102 model.
To solve this problem, the elders convened and decided that all of these cleaning devices should be standardized with an abstract cleaning interface.
This abstract cleaning interface allowed the new C-102 to work with any compatible cleaning device through this common interface. Unfortunately this still required a new S-102 to be designed to interface with C-102, but the designers were confident that no new cleaning devices introduced hereafter would require a new supervisor model.
... and order was once again restored in the galaxy...
... until the invention of a new transportation device known as legs which allowed droids to work in more complex environments.
At this point, the designers of the
C series of droids threw themselves off a cliff.
The elders convened and brought in a new design team to solve the problem. The new design team recognized that in order to avoid yet further and further modifications to the
S model, the
C cleaning droid model also needed an abstract interface through with the supervisor model would communicate. In addition, they realized a similar treatment was required for an abstract transportation interface.
... and cleanliness spread through the universe like a spring breeze (except, of course, that spring time did not exist in this galaxy)...
... until the invention of the hoverboard which made legs and wheels obsolete.
This caused the new design team to commit ritualistic light saber seppuku. The elders then brought yet another design team to replace the former one.
At this point, design team C realized that even though the supervisor droid did not have to change, the cleaning droid still needed programming to be aware of all the various types of cleaning and transport devices available for it to use (shown in red in above).
They also realized that the supervisor droid, who was merely programmed to routinely issue requests to clean rooms, was actually much easier to change than the cleaner droid, and had far less general applicability than a cleaner droid.
And thus, the designers decided to make cleaner droids accept requests to use any compatible cleaner device and any compatible transport device from a supervisor droid.
They also decided to use blue for abstract interfaces in their schematics, after realizing that they like blue.
... and thus was born the basic idea and primitive form of both dependency inversion and dependency injection, which would get further and further embellished in the future.
A curious thing happened with this new design. Instead of the cleaner robot being programmed to use these low-level cleaning and transportation devices directly, it was now being told what type of cleaning and transportation devices to use. Using its very limited A.I., the new prototype model of cleaning droid attempted to ask the designers what cleaning devices it should use now that it is no longer programmed to use specific ones. "Don't worry about it, your model will be told what to use when needed, but rest assured that a compatible cleaning and transportation device conforming to your abstract requirements will be supplied," said the designers.
A random passer-by then looked at this schematic and scribbled something obscure about efferent and afferent couplings yielding an instability metric while mumbling something cryptic about the ease at which packages can be modified.
The galaxy rejoiced with their new and flexible design until the designers realized that the supervisor droid was doing too much, and the elders realized that the designers and the factories they worked in should be included in the schematic.