In OOP, what are the differences between Afferent Coupling and Efferent Coupling?

5

Regarding the concept of acoplamento in Programação Orientada a Objetos , what are the differences between Acoplamento Aferente and Acoplamento Eferente ? If possible, to elucidate the understanding better, what would a possible code example and / or diagrama de classes be about them?

    
asked by anonymous 06.11.2015 / 18:31

3 answers

1

O acoplamento aferente (Ca) representa a contagem de quantas classes diferentes referem-se à classe atual, por meio de campos ou parâmetros.

For example, I have a class Animal and classes Pessoa and Jaula . The Pessoa and Jaula classes have a field of type Animal . There are two classes that refer to the Animal class. The afferent coupling of class Animal is 2 because two classes refer to this first.

public class Pessoa {
   public Animal animal;
}

public class Jaula {
    public Animal animal;
}

public class Animal {

}

O acoplamento eferente (Ce) representa a contagem de quantas classes diferentes a classe atual faz referência, por meio de campos ou parâmetros.

For example, I have a class Professor and classes Sala and Materia . The teacher class refers to the Sala and Materia classes. So the Teacher coupling is 2 because it references two classes.

public class Sala {
}

public class Materia {
}

public class Professor {
    public Materia materia;
    public Sala sala;
}

Source: Measuring Coupling

    
09.11.2015 / 13:28
1
  

Coupling between classes or subsystems is a measure of   interconnection between these classes or subsystems. Thus,   strong coupling means that related classes need to   internal details of each other, the changes propagate   by the system, and the system is potentially harder to understand.

     

In summary, the goals behind achieving a weak coupling between   classes and modules are:

     
  • Make code easier to read;
  •   
  • Making our classes simpler for other developers to consume, by hiding the ugly "inner workings"   Well-designed APIs;
  •   
  • Isolate possible changes to a small area of the code;
  •   
  • Reuse classes in completely new contexts.
  •   

(Source: Code metrics )

The afferent coupling (Ca) counts the number of classifiers out of a packet that references a classifier within the packet. If this number is high, this class has the high chance of being stable, decreasing the risk of coupling.

Theefferentcoupling(Ce)countsthenumberofclassifiersinadifferentpackagethanaclassifierinsideareferencepackage.Ifaclasshashighefferentcoupling,itmeansthatitdependsonmanyclasses.

Accordingtothe Elemar Jr :

  

Types with high efferent coupling are more difficult to test. Beyond   are more "sensitive" to changes in the system. Those with   high afferent coupling usually impact more seriously the   system when they do not work properly.


On the IBM website, you will find other metrics in the coupling category :

  
  • Abstractivity: This metric computes the ratio of abstract and interface classes to the total number of classes in the package.
  •   
  • Instability: This metric calculates the ratio of efferent coupling to total coupling (afferent afferent).
  •   
  • Normal distance: This metric calculates the normalized distance for the main sequence. The main sequence is where the abstractivity and   instability are balanced.
  •   

Reference:

17.10.2016 / 19:36
0

Oops!

Basically the difference is in the point of view regarding the class to which this analysis will be done. I explain:

Let's take as example the class "Example"

efferent coupling: Let's say the example class needs to send an email and post some information in an API. If you have a bad design, this class will depend efferent to these two classes:

class Exemplo{
   ClienteEmail clienteEmail;
   ClienteApi clienteApi;
}

That is, the class Example has coupling efferent to classes ClienteEmail and ClienteApi.

Afferent coupling are the classes that use the "Example class" example:

class Aplicacao{
   Exemplo exemplo;
}

class Teste{
   Exemplo exemplo;
}

That is, both Application and Test have an affine coupling to the Example class.

I know they were toy examples and I know how not applicable these examples are, but I hope I have elucidated your question a little.

A more detailed explanation you can find here: link

    
09.11.2015 / 13:36