这真的是适配器模式的一个例子吗?

发布于 2024-09-15 14:41:08 字数 747 浏览 5 评论 0原文

我有一个接口——“EventHandler”——声明了几个方法。

public interface EventHandler {
    void handleEvent1();

    void handleEvent2();

    void handleEvent3();

    void handleEvent4();
}

我还有一个类——“EventHandlerAdapter”——它实现了EventHandler。然而,它实际上并没有“实现”任何东西。要点是,如果另一个类想要实现 EventHandler,但不是它的所有方法,它可以简单地扩展 EventHandlerAdapter 并只重写它想要的方法。

public class EventHandlerAdapter implements EventHandler {
    public void handleEvent1() {}

    public void handleEvent2() {}

    public void handleEvent3() {}

    public void handleEvent4() {}
}

我不止一次见过这样的事情。 “EventHandlerAdapter”这个名字让我觉得它是适配器模式的一个例子......但真的是这样吗?我认为适配器的目的是将现有的实现转换为其他东西。我不明白这里的情况是怎样的。

如果它不是适配器模式的示例,那么它是什么?肯定已经发现了这样的事情。

I have an interface -- "EventHandler" -- that declares several methods.

public interface EventHandler {
    void handleEvent1();

    void handleEvent2();

    void handleEvent3();

    void handleEvent4();
}

I also have a class -- "EventHandlerAdapter" -- that implements EventHandler. However, it doesn't actually "implement" anything. The point is, if another class wants to implement EventHandler, but not all of its methods, it can simply extend EventHandlerAdapter and only override the methods it wants to.

public class EventHandlerAdapter implements EventHandler {
    public void handleEvent1() {}

    public void handleEvent2() {}

    public void handleEvent3() {}

    public void handleEvent4() {}
}

I've seen something like this on more than one occasion. The name "EventHandlerAdapter" suggests to me that it is an example of the adapter pattern... but is it really? I thought the point of an adapter was to translate an existing implementation into something else. I don't see how this is the case here.

If it isn't an example of the adapter pattern, what is it? Surely something like this has been identified.

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

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

发布评论

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

评论(4

゛时过境迁 2024-09-22 14:41:08

不,这不是适配器模式的示例,如下定义:

http://en.wikipedia.org /wiki/Adapter_pattern

但是,在 Java 事件处理中,正如您提到的那样,经常使用术语“适配器”。尽管“适配器”一词在两者中相同,但它们所指的并不是同一事物。 java.awt.event 包中出现的适配器可以轻松创建仅处理一个方法的事件处理程序,而无需编写一堆空方法。它们只是捷径课程。

Java 事件 API 通常对这些类具有一致的命名。当有 SomeEvent 事件类时,就会有一个 SomeListener 接口来监听事件,还有一个 SomeAdapter 类用空方法实现监听器接口。并非所有事件都具有这三个部分,但是这三个部分的命名和功能是一致的。

在您提供的示例中,我将重命名该类 EventAdapter 以与现有的 Java API 保持一致。

No, this is not an example of an Adapter Pattern, as defined here:

http://en.wikipedia.org/wiki/Adapter_pattern

However, in Java Event handling, the term Adapter is frequently used as you mentioned. Even though the word "Adapter" is the same in both, they do not refer to the same thing. The Adapters that appear in the java.awt.event package are there to make it easy to create an event handler that handles only one method without having to write a bunch of empty methods. They are only shortcut classes.

The Java Event API typically has consistent naming for these classes. When there is a SomeEvent event class, there is a SomeListener interface to listen to the event and a SomeAdapter class implementing the listener interface with empty methods. Not all events have all three of these parts, but there is consistency in the naming and function of the three.

In the example you provided, I would rename the class EventAdapter to be consistent with the existing Java API.

海拔太高太耀眼 2024-09-22 14:41:08

你是对的,这不是适配器模式的示例,而是一种广泛采用的约定,将“默认为空”称为“适配器”。

例如,java UI API 经常为 MouseListener 接口提供此类适配器。

You are right, it's not an example of adapter pattern but a widely adopted convention to have "default empty for " called "adapters"

For example java UI APIs often provide such adapters for MouseListener interfaces.

幸福%小乖 2024-09-22 14:41:08

你是对的,这不是适配器设计模式的示例,而是接口的一个简单的默认实现。我会将其重命名为 DefaultEventHandlerEmptyEventHandlerGenericEventHandler

You are right, this is not an example of the Adapter design pattern, rather a trivial default implementation of the interface. I would rename it to DefaultEventHandler, EmptyEventHandler or GenericEventHandler.

×纯※雪 2024-09-22 14:41:08

AWT 有很多接口的实现,他们称之为“适配器”,例如“MouseAdapter”、“FocusAdapter”。不,它们不是适配器模式的实现。它们是便利类,我简单地将它们称为存根

The AWT has a lot of implementations of interfaces which they call "Adapter", like 'MouseAdapter', 'FocusAdapter'. And no, they are not implementations of the Adapter pattern. They are convenience classes, I'd simply call them stubs.

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