Interface, Layer Interconnection

2

Is the use of the interface only in the interconnection of layers? whenever I want to communicate for example the model layer with the presenter , will I need an interface?

    
asked by anonymous 25.11.2014 / 12:01

1 answer

4
  

Is the use of the interface only in the interconnection of layers?

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

Example of another usage: You can set properties, signature methods and events on your interface, so that it is implemented in different objects as you need them.

To illustrate, suppose that two banks (BancoA and BancoB objects) need to apply a discount (behavior common to both objects: apply discount), but discount calculation is different for 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 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 the model layer with the presenter, will I need an interface?

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

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

    
25.11.2014 / 12:30