ActionListener 是委托模式的一个示例吗?

发布于 2024-12-26 02:52:31 字数 428 浏览 3 评论 0原文

在我的大学时代,我从未意识到 Java API 中有哪些模式。 现在在工作中,我在 iOS 上的 Objective C n Cocoa 中遇到了委托模式,其中一个屏幕将自己设置为即将到来的屏幕上的委托,以便该屏幕可以将一些消息传递给该委托,并且当它返回到上一个屏幕时可以采取一些操作屏幕。

我意识到我以前做过类似的事情,当我将“this”作为 ActionListener [通过实现接口]传递给 JButton 时,它会自动调用我在此类中实现的 actionPerformed,因此我可以更改任何实例数据在我的 JFrame 类中。

那么 ActionListener 是 Delegate 的一个例子吗?如果我是对的的话?

编辑:正如下面正确提到的,它是观察者模式。我们不设置 ActionListener,而是添加一个。因此,该操作可以有许多侦听器。

In my college days, I never realized what patterns were there in the Java API.
Now at work I came across Delegation pattern in Objective C n Cocoa on iOS where one screen sets itself as a delegate on coming screen so that that screen can pass some message to that delegate and it can take some action when it comes back to the previous screen.

I realize that I use to do something similar with when I used to pass "this" as as ActionListener [by implementing the interface] to a JButton and it would automatically call actionPerformed implemented by me in this class and thus I could change any instance data in my JFrame class.

So Is ActionListener an example of Delegate If I am correct ?

EDIT: As correctly mentioned below, It is Observer pattern. We dont set ActionListener we add one. Thus there can be many Listeners to that action.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兲鉂ぱ嘚淚 2025-01-02 02:52:31

ActionListener观察者模式的示例。您可以在组件上注册观察者(或侦听器),当特定事件发生时将调用这些观察者(或侦听器)。

ActionListener is an example of the observer pattern. You register observers (or listeners) on a component that get called when a specific event occurs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文