Coupling and cohesion are common terms which occur together frequently when it comes to software design and object-oriented programming.
Coupling is a measure of how much a component knows about another component in terms of the other components inner workings or inner elements, that is, how much knowledge one component has of another component.
Coupling is the degree of interdependence between software modules; a measure of how closely connected two routines or modules are; the strength of the relationships between modules.Wikipedia
Tight coupling indicates that when components are connected together, those components are tied together in a way that it is not possible to change one without changing the other.
Consider the below code base.
From the above it can be seen that a concrete implementation of the
Logger class is used within the
EmailValidator class. Also, the
EmailValidator is responsible for creating an instance of the
Logger class requires dependencies in a future feature, then the
EmailValidator class will definitely break and will be required to provide these dependencies in order to create a new instance of the
Logger class. This is a bad design and the two components are tightly coupled since the
EmailValidator class is required to fully understand the inner workings of the
Loose coupling or low coupling indicates that when components are connected together, those components depend on each other to the least extent practically possible.
Consider the below code base.
EmailValidator class is using an
ILogger interface and not a concrete implementation of the
EmailValidator can focus on its intended behavior of validating email addresses and does not need to worry about constructing a
Logger instance. The two components,
Logger are now loosely coupled and use the
ILogger interface as a contract to communicate with one another.
Cohesion is a measure of how the functions or elements within a single component are related.
Cohesion refers to the degree to which the elements inside a module belong together.Wikipedia
Low cohesion means that a given component performs features which are not related to it, hence creating problems as the component grows.
Consider the below class.
BusinessService class is an example of low cohesion since only one class is responsible for containing logic for different types of features. This class reduces re-usability and maintainability.
High cohesion or highly cohesive means that functions or elements of a component are co-dependent and are strongly related to the feature the component is exposing.
Consider the below classes.
Separate classes are created for each feature, i.e.
VillainService. This design is highly cohesive which increases re-usability and maintainability.
Coupling and cohesion are import concepts to understand when it comes to software design and object-oriented programming. Software components should be designed with low coupling and high cohesion.
Low coupling indicates that a system is well structured with a good design and when combined with high cohesion, the system will be highly understandable, robust, reusable, and maintainable.
Further information on coupling and cohesion can be found at the following links: