Nemanja
Nemanja

Reputation: 218

Performance: Inner Interface vs separate Interface class

In my android project I use 4 Recyclerviews in 4 different Activities and I need 3 methods onClick, onButtonClick and onDeleteClick. Some methods are used and some are not. First, I created an inner interface in every single Activity that I needed it, and with them a proper methods. But because they are repeating, I created a separate interface class with all 3 methods and then reference it in my 4 Activities. I override methods that I need and the ones that I dont, I just left empty body. What is the best/preferred way?

1.

public class A {
                private OnItemClickListener mListener;

                public interface OnItemClickListener {
                    void onDeleteClick(int position);
                }

                ...
                @Override
            public void onDeleteClick(int position) {
                errase(position);
            }
}

2.

public interface OnItemClickListener {
        void onItemClick (int position);
        void onDeleteClick (int position);
        void onButtonClick (int position);
    }

and

public class A {
     private OnItemClickListener mListener;

     ...
     @Override
            public void onItemClick(int position) {
                 //empty because I dont need it here
            }

            @Override
            public void onButtonClick(int position) {
                 //empty because I dont need it here
            }

            @Override
            public void onDeleteClick(int position) {
                errase(position);
            }
}

Upvotes: 0

Views: 107

Answers (2)

Stephen C
Stephen C

Reputation: 718826

To my mind, there is no "best/preferred" way:

  1. Since nested interfaces are implicitly NOT inner (they are implicitly static) there is no hidden reference to an instance of the enclosing class. Therefore there should be zero performance difference between normal and nested interfaces.

  2. Java style guides do not / will not recommend one form over the other as a universal statement.

  3. I've not heard of anyone trying to do a scientific usability study on this.

  4. I've not heard of anyone trying to scientifically measure developer opinion on this.

  5. That leaves us with "random StackOverflow contributer's opinions", which I wouldn't trust to guide me on this. (Everyone's mental processes when reading code are different ...)

Bottom line: nothing.


It is up to you to decide which you think will be more readable / maintainable, given the actual context. But:

  • I would recommend that you be consistent in your choice.

  • If your APIs are likely to be used / maintained by others, it is a good idea to discuss it with them. Getting a consensus with co-workers would be a good thing, or even an "agree to disagree" and then move on.

But don't get hung up on this because it is definitely a small issue.

Upvotes: 0

irfan
irfan

Reputation: 896

You can create an adapter class and provide empty implementation for all the methods declared in an interface, this will reduce the duplicate lines of code and then extend it. (though not sure if this is the best way for java 8 or more) in your code somewhat like

interface OnItemClickListener {
    void onItemClick(int position);
    void onDeleteClick(int position);
    void onButtonClick(int position);
}

abstract class Adapter implements OnItemClickListener {
    public void onItemClick(int position) {};
    public void onDeleteClick(int position) {};
    public void onButtonClick(int position) {};

}

public class A extends Adapter
{
    @Override
    public void onDeleteClick(int position) {
        errase(position);
    }
}

Upvotes: 2

Related Questions