2

Listener, Interface, Handler.

They all sound similar but they must be different.

Maybe 'delegate' in objective-c as well.

eugene
  • 39,839
  • 68
  • 255
  • 489
  • This stack overflow question has good answers for that. http://stackoverflow.com/questions/4725241/whats-the-difference-between-event-listeners-handlers-in-java – MysticMagicϡ Nov 19 '12 at 06:19

3 Answers3

6

Interface:

interface are used to provide roles. According to Design Patterns when there are certain behaviors that keeps changing must be encapsulated in an interface or an abstract class.

Eg:

Suppose there is this paint class, which has a method paintIt().

Now the method paintIt() can be free-hand, shading etc, so its kind of behavior which keeps changing. So we need to encapsulate it in an interface or an abstract class.

Listener:

Listeners are the interface that listens to certain action, and they contains the call back methods, which has the logic that is to be fired when that particular action is done.

Handler:

Handlers in android are used to post the data from the Non-UI thread to the UI thread.

Community
  • 1
  • 1
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
1

Listener is is almost similar kind of delegate in dot net and may be objective C. Its main use is to hold the call back method when some action or event is performed. In the case of dotnet, we are passing the method itself as call back to the delegate (probably in Objective C, the case will be same.), but in java, there is no conscept like a delegate. So it is using the Interface to do the same trick.

For example, If we need to assign a button click to a method, in dotnet and probably in objective C, we will be passing that function pointer to the corresponding click delegate. In Java, what we do is we will be creating an interface like this :

public interface ButtonClickListener{
    void onClick();
}

Then inside the Button class, instead of delegate, we will be creating a function to accept this listener :

public void setOnClickListener(ButtonClickListener listener){
    mButtonClickListener=listener;
}

We will call this setOnClickListener method in our class with object of the ButtonClickListener implemented class.

Then whenever the Button click happens, what the Button class do is :

mButtonClickListener.onClick();

This will invoke the onClick of the ButtonClickListener Implemented class and we can perform the required operations over there.

Handler is not related with this. This is an Android framework provided class to sync operations with the UI thread when we are working with other custom threads.

Hope you got the idea!!.

SteD
  • 13,909
  • 12
  • 65
  • 76
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
1

This is tricky because there is so much overlap, and especially referring to Objective-C.

Here's my best effort:

A listener is a single variety of an interface.

Check out http://developer.android.com/guide/topics/ui/ui-events.html#EventListeners

According to that page, what makes a "event" listener unique is it's a single-callback interface. That page is specific to a view, but of course a listener can apply to any other type of event.

The android page for android.os.Handler is: http://developer.android.com/reference/android/os/Handler.html

Perhaps it's reasonable to say a handler is more of dual-purpose concept, if you're looking very specifically at differences. A handler comprises sending a message/event, and receiving it, later on.

Baseball?
A player out in the field is a listener for a ball. A glove (weak analogy) on the player's hand is the interface. A player tossing the ball in the air to himself or herself for fun and catching it is a handler.
:)

In Objective-C: An interface is mostly the same in its use, but has a different name: @protocol

Android/Java's Listener into Objective-C is implemented in a variety of ways, like a listener in java/android anyway. The best Obj-C counterpart I'd say is a notification (NSNotification and NSNotificationCenter). This could possibly be argued.

There are various ways to code of course, and various patterns. A Handler in android could be equated to a block passed as an argument in an Obj-C message, or a delegate would fit too.

Tom Pace
  • 2,347
  • 1
  • 24
  • 32