Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle an event: implement an interface or using inner class to handle the interface. Which is better

Tags:

java

oop

android

To handle an event, there are 2 ways:

  1. Implementing the callback interface, for example

    public class A implements View.OnClickListener {
        public void onClick(View v) {
            ....
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ...
            aboutLayout = (LinearLayout) findViewById(R.id.aboutLayout);
            aboutLayout.setOnClickListener(this);
        }
    }
    
  2. Creating an inner class which implement the callback interface

    public class ActivityAbout  {
        private class ViewClickListener implements View.OnClickListener {
            public void onClick(View v) {
                ..
            }
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ...
            ViewClickListener listener = new ViewClickListener();
            aboutLayout = (LinearLayout) findViewById(R.id.aboutLayout);
            aboutLayout.setOnClickListener(listener);
        }
    }
    

Which one is better?

like image 794
thuanle Avatar asked Sep 09 '12 19:09

thuanle


2 Answers

The choice depends entirely the way you plan to use the class, each method is valid and has their corresponding strengths and weaknesses:

  • Method 1 is quick to code but limits you to one onClick() function, which may or may not become an organizational mess.
  • Method 2 allows you to create multiple onClick() functions and use them in more logical methods.

But I would like to present Method 3, an anonymous callback:

public class MyActivity extends Activity {
    OnClickListener myClickListener1 = new OnClickListener() {
        public void onClick(View v) {
            ..
        }
    }

    OnClickListener myClickListener2 = new OnClickListener() {...}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        view.setOnClickListener(myClickListener1);
    }
}

Notice that unlike Method 2, I did not create a custom class. I would avoid creating a new custom class unless you are going to add more functionality, keep local variables, or otherwise modify the basic function of an OnClickListener.

like image 77
Sam Avatar answered Oct 10 '22 07:10

Sam


I tend to use an inner class if either the code is very long (and needs to be organized or methodized), or if I plan to use the same code over and over. Even my Surface callbacks are organized as inner classes, just for clarity's sake.

I use an anonymous class if the code is short and easy to handle (perhaps just one line within the event).

I only ever use the first implementation if the events are directly related to the class itself, and will never need other listeners attached to it. This is a very rare circumstance. (I also use it for simple debugging, but that's kind of irrelevant here.)

like image 32
Cat Avatar answered Oct 10 '22 09:10

Cat