Objective-C 委托与 Java 侦听器
我读过很多关于 Objective-C 代表的文章和读物,试图理解它们。来自 Java,它们看起来非常像 Java 监听器。例如,假设我有一个 Java 按钮。当按钮被按下时,我希望发生一些事情。我的代码可能看起来像这样:
ButtonListener myButtonListener = new ButtonListener();
someButton.addActionListener(myButtonListener);
...
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
类似的东西。在 Objective-c 中,我似乎会做一些类似于为我的按钮调用 setDelegate 方法并将其作为委托传递给“监听器”的事情。然后,实际的按钮类可能会检查该委托是否响应某个选择器(即actionPerformed)。如果我以正确的方式思考这一点,那么代表们确实就像听众一样。这是正确的吗?有什么重大差异吗?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你几乎已经按下按钮了。唯一真正的区别是 obj-c 中的委托通常实现多个函数来对与它们所委托的对象相关的事件执行各种操作。例如, UITextViewDelegate 有以下方法:
我发现的唯一真正的区别是你不能像在 java 中那样内联创建委托:
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:
The only real difference I've found is you can't create your delegates inline, the way you can in java like:
它们相似,但不完全相同。委派模式具有更广泛的定义,并且通常是实现定义的任务,这些任务可以超出仅侦听的范围。任务可以包括监听,或者委托的实现可以被定义为监听(独占)。
objc 委托通常用于避免子类化,并充当侦听器或数据提供者。委托的作用是由协议定义的 - 它的作用不仅仅是侦听器。因此代表可以是数据源/提供者。它只是将实现卸载到另一个类的一种方法,从类中删除经常定制的、特定于应用程序的实现。
NSButton/UIButton 已经通过目标+操作机制专门用于这种情况。对于这种特定情况,您可以使用目标+操作。
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.
委托类似于侦听器或观察者,协议类似于接口,除了协议可以定义可选函数(也称为消息)。在 Objective C 中,您可以使用类别来扩展现有类(没有其源代码)以采用协议并使其成为委托,这样您就根本不需要创建新的匿名内部类。在 Java 中你不能这样做。
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.
我认为在 java.util.concurrent pagkage 中可以找到对 .NET 委托更好的 Java 模拟:Callable、Future、Executor。
I think that a better Java analog to .NET delegates would be found in the java.util.concurrent pagkage: Callable, Future, Executor.