A dependence is a liability: it expands the scope of what needs to be considered.
The most effective way to reduce coupling is:
To design the parts so that they are simple and well defined.
Bring together aspects of the system that belong together and separate aspects that don’t.
There are also some tactics that can be applied when you already have a candidate decomposition: they involve introducing new parts and altering specifications.
For now, we’ll just mention some briefly to give you an idea of what’s possible:
Facade
The facade pattern involves interposing a new implementation part between two sets of parts. The new part s a kind of gatekeeper: every use by a part in the set S of a part in the set B which was previously direct now goes through it.
Example: introduce a facade that sits between the layers, collects together all the networking that the protocol layer needs (and no more),and presents them to the protocol layer with a higher-level interface.
Hide Representation
A specification can avoid mentioning how data is represented.
This kind of specification weakening is known as ‘data abstraction’.
By eliminating the dependence of the using part A on the representation of data in the used part B, it makes it easier to understand the role that B plays in A. It makes it possible to change the representation of data in B without any change to A at all.
Polymorphism
A program part C that provides container objects has a dependence on the program part E that provides the elements of the container.
To reduce the coupling between C and E, we can make C polymorphic.
The word ‘polymorphic’ means ‘many shaped’, and refers to the fact that C is written without any mention of special properties of E, so that containers of many shapes can be produced according to which E the part C uses.
Callbacks
In the browser, a GUI part might depend on the Main part because it calls a procedure in Main when, for example, a button is pressed.
This coupling is bad.
Instead, the Main part might pass the GUI part at runtime a reference to one of its procedures. When this procedure is called by the GUI part, it has the same effect it would have had if the procedure had been named in the text of the GUI part.
But since the association is only made at runtime, there is no dependence of GUI on Main.
There will be a dependence of GUI on a specification (Listener, say) that the passed procedure must satisfy.
Copyright © 1998-2009 Dilvan Moreira