Interface, interlinking of layers

Asked

Viewed 172 times

2

The use of the interface is only in the interlinking of layers? whenever I want to communicate for example the layer model with the presenter, I’ll need an interface?

1 answer

4


The use of the interface is only in the interlinking of layers?

You can use interfaces between layers if you want to maintain a low coupling/dependency between layers. But the use is not limited only to this.

Example of another use: You can define properties, methods signature and events in your interface, so that it is implemented in different objects as your need.

To illustrate, suppose that two banks (Bancoa and Bancob objects) need to apply a discount (common behavior to both objects: apply discount), but the calculation of the discount is different in each object.

Once the Bancoa and Bancob objects implement an interface with this behavior, you can define an Interface, which will be a contract that each object can implement/define its own discount calculation formula.

Interface:

interface IPagamentoDesconto
{
    double AplicarDesconto(double valor);
}

Class that implements the Ipagamentodesconto interface:

public class BancoA : IPagamentoDesconto
{
    public double AplicarDesconto(double valor)
    {
        //Pagamento com desconto calculado de uma forma 
    }
}

public class BancoB : IPagamentoDesconto
{
    public double AplicarDesconto(double valor)
    {
        //Pagamento com desconto calculado de uma outra forma 
    }
}

whenever I want to communicate for example the model layer with the presenter, I will need an interface?

It’s an alternative and good practice if you want to maintain a low coupling/dependency between layers model and presenter.

Coupling means how much one class depends on the other to function. And the greater this dependence between the two, we say that these classes are strongly coupled. A strong coupling, makes it very expensive to maintain and manage, as any change will affect the entire class chain.

Browser other questions tagged

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