When methods of a class access (depend on) many operations across an excessive number of classes, it is said that the class is dispersedly coupled with those provider classes.

Impact

  • A change in a dispersively coupled method leads to changes in all the dependent (coupled) classes.

Characteristics

  • Methods of affected classes call too many methods from other classes
  • Communication with each of the external classes (that the affected class is coupled with) is not intense. The affected method calls one or few methods from each class it is coupled with.
  • Calling methods have the non-trivial level of nested conditionals (nested IF-ELSE statements).

Example(s)

  • Method A is the caller class is calling Method 1 from Service 1, Method 2 Service 2, Method 3 Service 3 and so on.

Guidelines

  • Avoid calling methods from unrelated classes.
  • In case of Intensive Coupling, multiple calls from the provider class can be combined into a smarter service that lives in the provider class and internally forwards calls to individual operations.
public class BankAccount {
  private AccountInfo acInfo;

  public Boolean isBankAccountValid()
    if(acInfo.condition1() == true) && (acInfo.condition2()==true)
    (acInfo.condition3() == true ) && ( acInfo.condition4() == true ) {
    // execute some code......
    }
  }
}
  • Change the above implementation to:
public Boolean isBankAccountValid {
  return acInfo.isValid();
}
  • In case of Dispersed Coupling (one method calling into methods of too many other classes) it is likely that the calling method is a Brain Method that does too many non-cohesive tasks. Try to split such a method.