I’m wondering if someone might explain the last paragraph written in this example on Dependency Inversion & Decoupling.
http://www.springbyexample.org/examples/core-concepts.html
Following the example code, understanding the reasoning behind decoupling classes by implementing a base class is fine.
What I would like some clarification on is the last paragraph:
However, there is one problem with this implementation. We don’t have a main method. We > definitely need one in order to run our
application, and somewhere in this main method we will need to
instantiate the LocalVoteRecorder.By instantiating the LocalVoteRecorder in our main method, we would
break Rule #1 of Dependency Inversion. We have coded to the
abstraction, we have integrated our changes, but our application would
still have a dependency on a lower level class.
Does this mean it is not possible to create a truly decoupled application? How can it be that one could write a 100% decoupled application? Is it even necessary, and are there any best practices I could read up on.
Many thanks
1
It is very hard (although in a language that has reflection not entirely impossible) to create a 100% decoupled application. With reflection, we can use this to allow the user to specify concrete classes for each object needed in the application at runtime (perhaps through a configuration file), thus achieving complete decoupling at the expense of increased complexity.
But a more important question to ask is why you would want to.
We try to decrease coupling in most of our code because coupling makes change harder, by causing a single change to have consequences for all of the code that is coupled to the module that was changed.
But if we group together all of the coupling in a single location, this problem is mitigated. A change to a module’s behaviour may also require a change to the code that initialises the module, but because all such code is kept together no further changes propagate out of that initialisation code, so we don’t experience the negative effects. This is good enough for almost all applications.
3
Tight coupling is bad. Loose coupling is what need. And nothing works in complete Isolation, so dependency (to interface) is inevitable.
The best practices is to create decoupled components as described here to avoid the disadvantages of tightly coupled application as mentioned on this Wikipedia page. Consider below lines too as they metioned here:
A module is closed if it has a well defined stable interface that all
other modules must use and that limits the interaction and potential
errors that can be introduced into one module by changes in another.
Thanks.
Sort of. You can’t avoid the coupling altogether. The best you can do is trade one kind of coupling for a generally more desirable kind. Instead of two concrete objects coupled with each other, they are each coupled to an abstract interface.
Also, you have to depend on a concrete implementation at some point. Just because those dependencies are not resolved until runtime doesn’t mean they don’t exist. Sometimes the programmer doesn’t care which concrete implementation is used, but usually they need a way to specify it somehow, which means a configuration file.
So, while you can have a program without any concrete dependencies at compile time, usually it’s not worth it unless you have some specific need to substitute specific classes at runtime.
I’m wondering if someone might explain the last paragraph:
The author says that having “Dependency Inversion & Decoupling” alone is not enought.
He means that “wiring the applicaton” by implementing a “main” method that constructs all classes is one option tu use the “independant modules”
In the next chapter he introduces, how to make “wiring the applicaton by code” a lot easier by introducing “Dependency Injection”
And later he introduces the spring-ioc that can do most of the wirining for you.
Does this mean it is not possible to create a truly decoupled application?
No but a ioc like spring can make it a lot easier if you have understood the principles behind.