Leave some data from a non-mandatory java interface

1

I use interfaces to call 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);
}

Only sometimes some methods that have it are not needed. For example: let's assume I'm going to save a user, then not the need for me to see the results of fetching, listing, and deleting.

Can you make them non-mandatory, or just import what is expected of the return?

    
asked by anonymous 22.10.2016 / 14:20

1 answer

3

This is not possible in Java, nor is it possible in any language (except dynamics) that I know of. This is contrary to the idea of interface, since the concept of interfaces implies that a class that implements an interface guarantees, as in a contract, compliance with the interface as a whole and not partially. Failing to implement part of an interface is equivalent to breaching a contract! The "caller" of an interface does not have to worry if the implementor of this interface only deals with this or that method only and can assume that it "knows" how 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 the necessary ones for each case. This has several advantages because it allows the code that "calls" the callback to inspect whether the last class implements, or not one of the interfaces, just calling the implemented ones, and prevents you from having to implement all methods in your class. It 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);
}

So, 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) {
    }
}

An android activity that allows users to be excluded could be implemented like this:

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 methods you decide. As I do not know your architecture, I just used example names, the important thing is to understand the concept!

And you can also create interfaces that extend more than one of the interfaces above, allowing you to combine the above interfaces into groups that make it easier to define the interfaces that a class implements. Your initial CallbackUser would be defined as follows:

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) {
    }
}
    
22.10.2016 / 20:27