JPC
JPC

Reputation: 8296

Objective-C delegates vs Java listeners

I've read a bunch of articles and readings on Objective-C delegates, trying to understand them. Coming from Java, they seem very much like Java listeners. For example, let's say I had a button in Java. When the button is pushed, I want something to happen. My code might look something like this:

ButtonListener myButtonListener = new ButtonListener();    
someButton.addActionListener(myButtonListener);
...

class ButtonListener implements ActionListener
{
   public void actionPerformed(ActionEvent e)
   {

    }
}

Something like that. In objective-c it seems that I would do something along the lines of calling a setDelegate method for my button and passing it the "listener" as a delegate. The actual button class would then probably check if that delegate responded to some selector (ie. actionPerformed). If I'm thinking about this the right way, it does seem like delegates are just like listeners. Is that correct? Are there any major differences?

Thanks!

Upvotes: 19

Views: 7315

Answers (4)

jack
jack

Reputation: 731

Delegate is similar as listener or observer, protocol is similar as interface except protocol can define optional functions (aka messages). In Objective C, you can augment an existing class (without its source code) using category to adopt a protocol and make it a delegate, so that you don't need to create new anonymous inner classes at all. You can't do that in Java.

Upvotes: 1

duffymo
duffymo

Reputation: 308928

I think that a better Java analog to .NET delegates would be found in the java.util.concurrent pagkage: Callable, Future, Executor.

Upvotes: 0

justin
justin

Reputation: 104698

they are similar, but not identical. a delegation pattern has broader definition, and often implementation defined tasks which can extend beyond listening alone. the tasks can include listening, or the delegate's implementation may be defined as listening (exclusively).

objc delegates are often used to avoid subclassing, and to serve as listeners or data providers. what a delegate does is defined by the protocol - it can serve as much more than a listener. so a delegate could be a data source/provider. it's just a means to offload implementation to another class, to remove from the class what is frequently customized, app-specific implementation.

NSButton/UIButton's already been specialized for this case via target+action mechanisms. you'd use target+action for this specific case.

Upvotes: 8

Dan F
Dan F

Reputation: 17732

You're pretty much on the button there. The only real difference is delegates in obj-c generally implement multiple functions to perform various actions on events regarding the object they are delegating. For example, the UITextViewDelegate has the methods:

– textViewShouldBeginEditing:
– textViewDidBeginEditing:
– textViewShouldEndEditing:
– textViewDidEndEditing:

The only real difference I've found is you can't create your delegates inline, the way you can in java like:

someButton.setOnClickListener ( new View.OnClickListener {
    @Override
    public void onClick() {
        //do stuff
    }
});

Upvotes: 10

Related Questions