Leave some data from a Java interface not required

Asked

Viewed 51 times

1

I use interfaces to make the callback on android.

public interface CallbackUsuario {
    void resultadoSalvar(boolean b);
    void resultadoTrazer(Usuario u);
    void resultadoListar(List<Usuario> lista);
    void resultadoExcluir(boolean b);
    void falha(String f);
}

It’s just that sometimes some of the methods you have there aren’t necessary. For example: let’s assume that I will save a user, then not the need for me to see the results of bring, list, and delete.

You can leave them not obligatory, that is to say, it only matters what is expected of the return?

1 answer

3


This is not possible in Java, nor is it possible in any language (except the dynamics) I know. This is contrary to the idea of interface, because the concept of interfaces implies that a class that implements an interface ensures, as in a contract, the fulfillment of the interface as a whole and not partially. Failing to implement part of an interface is tantamount to breaking a contract! The "caller" of an interface need not worry whether the implementor of this one or that one method only and may assume that it "knows" to handle the entire interface.

However, since a Java class can implement multiple interfaces, I would segregate these interfaces into different interfaces and only implement those necessary for each case. This has several advantages, as it allows the code that "calls" the callback to inspect whether the passed class implements, or not, one of the interfaces, just by calling the implemented ones, and prevents you from having to implement all the methods in your class. Would do something like this:

public interface ICallbackSalvamentoUsuario {
    void resultadoSalvar(boolean b);
}
public interface ICallbackResultadoUsuario {
    void resultadoTrazer(Usuario u);
}
public interface ICallbackLeituraUsuario {
    void resultadoListar(List<Usuario> lista);
}
public interface ICallbackResultadoExclusaoUsuario {
    void resultadoExcluir(boolean b);
}
public interface ICallbackFalhaUsuario {
    void falha(String f);
}

Thus, a class that allows saving, listing and the result of a user would look like this:

public class MinhaClasseUsuario implements 
    ICallbackSalvamentoUsuario,
    ICallbackResultadoUsuario,
    ICallbackLeituraUsuario  {

    public void resultadoSalvar(boolean b) {
    }
    public void resultadoTrazer(Usuario u) {
    }
    public void resultadoListar(List<Usuario> lista) {
    }
}

Already an android activity that allows the deletion of users could be implemented so:

public class AtividadeExclusao extends AppCompatActivity 
    implements 
        ICallbackExclusaoUsuario,
        ICallbackLeituraUsuario {

    public void resultadoExcluir(boolean b) {
    }

    public void resultadoListar(List<Usuario> lista) {
    }
}

Of course, the name of the interfaces and the methods you decide. As I do not know your architecture, I only used example names, the important thing is to understand the concept!

And you can also create interfaces that extend more than one of the above interfaces, allowing you to combine the above interfaces in groups that make it easy to define the interfaces that a class implements. Your initial Callbackuser would thus be defined:

public interface ICallbackUsuario extends 
    ICallbackSalvamentoUsuario,
    ICallbackResultadoUsuario,
    ICallbackLeituraUsuario,
    ICallbackResultadoExclusaoUsuario,
    ICallbackFalhaUsuario {
}   

public class XYP implements ICallbackUsuario {

    public void resultadoSalvar(boolean b) {
    }
    public void resultadoTrazer(Usuario u) {
    }
    public void resultadoListar(List<Usuario> lista) {
    }
    public void resultadoExcluir(boolean b) {
    }
    public void falha(String f) {
    }
}

Browser other questions tagged

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