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

Asked

Viewed 698 times

5

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

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 the classes Pessoa and Jaula. The classes Pessoa and Jaula has a field of the type Animal. Are two classes that refer to the class Animal. The afferent coupling of the class Animal is 2 because two classes reference 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 the classes Sala and Materia. The teacher class references the classes Sala and Materia. 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

1

Coupling between classes or subsystems is a measure of interconnection between these classes or subsystems. Therefore, Strong coupling means the related classes need know internal details of each other, the changes propagate system, and the system is potentially more difficult to understand.

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

  • Make code easier to read;
  • Make our classes simpler for other developers' consumption by hiding the ugly part" of the inner workings in Well designed Apis;
  • Isolate possible changes in a small area of code;
  • Reuse classes in completely new contexts.

(Source: Code Metrics)

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

inserir a descrição da imagem aqui

The efferent coupling (Ce) counts the number of classifiers in a different package than a classifier within a reference package. If a class has high efferent coupling, it means that it depends on many classes.

inserir a descrição da imagem aqui

According to the Elemar Jr:

Types with high efferent coupling are more difficult to test. In addition are more "sensitive" to system modifications. Those with high afferent coupling usually impact more seriously the system when not functioning properly.


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

  • Abstractivity: This metric calculates 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 for the total coupling (more efferent afferent).
  • Normal distance: This metric calculates the normalized distance to the main sequence. The main sequence is where abstraction and instability are balanced.

Reference:

0

Opa!

Basically the difference is in the point of view in relation to the class to which this analysis will be made. Explain:

Let’s take as an 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 have efferent dependency to these two classes:

class Exemplo{
   ClienteEmail clienteEmail;
   ClienteApi clienteApi;
}

That is, the Example class has efferent coupling to the Clienteemail and Clienteapi classes.

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 matching to the Example class.

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

A more detailed explanation you can find here: http://leandrodaniel.com/index.php/code-metrics-parte-3-medindo-acoplamento/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.