This article explains coupling between classes. It also explains the consequences of high coupling. The article then classifies coupling and explains each type of coupling along with the ways to eliminate coupling.
Coupling is the factor of dependency of one class on another class. The factor of coupling varies between low and high. Systems having high coupling are unstable and require more effort to modify. In a system with high coupling factor, modification in one class forces a ripple of changes in other classes and thus increases the effort required for modification. High efforts require more cost. High coupling also decreases the reusability of a class because the dependant class must be included. Classes with high coupling are difficult to understand in isolation. Systems having low coupling are stable and when a change is made in one class, it will not require change in the implementation of other classes.
Coupling is classified as follows.
Content coupling exists between two classes when one class relies on the internal working of another class, i.e., one class is using the variables of another class. When a class holding content is modified to contain another format of the content then other class using the content should be modified. The coupling factor for content coupling is high. This form of coupling is undesirable because any class can modify the internal data of another class. To reduce use private access control for the contents in the class and provide accessing methods such as getVal and setVal.
Common coupling exists when classes in the system share a global data. Changing the format of the shared resource requires modifications in all the classes using the global data. To reduce common coupling, create a class to contain global data and add public methods in the class to obtain or modify the global data. One should use critical sections in the class.
External coupling is a dependency of the class on third party classes like MFC, QT, etc. or shares an externally imposed data format, communication protocol or device interface. External coupling can be reduced by using façade pattern.
Control coupling is when one class controls the logic of another class, by passing it information on what to do, for example, by passing flag or command, thus controlling the sequence of execution. To reduce use polymorphic operations or use a lookup table that will map a command to a method.
Stamp coupling is a form of coupling in which two classes modify or access data in the same object. To reduce add methods to access or modify all data within the object.
Data coupling is the form of coupling in which one class passes simple data to another as an argument. Coupling is due to interpretation of data, i.e., meaning of data that is passed as argument to the member function of another class. If the meaning of the data that is passed as argument is changed, then the other class has to modify to accommodate the change. To reduce developers should not pass unnecessary arguments and should pass few arguments, each containing more abstract information.
Message coupling is the lowest possible coupling. Classes are not dependent on each other instead they use a public interface to exchange parameter-less messages (or events).
Routine Call Coupling
Routine call coupling is a form of coupling in which one routine calls another. To reduce the total number of routines that a particular class calls must be reduced. If there is a sequence of two or more routines to compute something and this sequence is used in more than one place, then to reduce routine call coupling a single routine must be written that encapsulates the sequence.
Inclusion coupling is a form of coupling in which one component includes source code of another component. If the included component changes something on which includer depends or adds something that raises the conflict with something in the includer, then the includer must change. To reduce, do not include components that are not required.
- Version 1: Initial article without examples