Coupling
Coupling is a measure of the dependency between two components. It’s the
degree to which one component relies upon the other component. In terms of
software development, coupling is how much a "using" component needs to
know about the internals of the used component and the published interface
(the public methods and attributes) of the used component.
Here’s an example of loose coupling versus tight coupling. In the Run method
of the MakeTea class, I tested whether the kettle was boiling as follows:
While(![Kettle IsBoiling]);
In the above example, the recipe program doesn’t know how the Kettle class
implements IsBoiling (that is, this implementation is hidden). The Kettle
class can change this implementation without the recipe program having to
change. This is an example of loose coupling achieved by information hiding.
The same test could have been written as follows:
While(![Kettle [Whistle IsSounding]]);
Here, the recipe program assumes that the kettle has a whistle. In other
words, it’s utilizing an internal secret of the kettle, and is therefore coupling
itself to kettles that have whistles. If I were to use a kettle that has some
other means of signaling that it’s boiling (for example, by emitting steam), I
would have to rewrite the recipe program. The fact that I have to rewrite the
recipe program is a symptom of tight coupling between recipe program and
the kettle.
No comments:
Post a Comment