本文介绍了MouseAdapter:它使用哪种模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够找到很棒的资源,告诉我,Java API的MouseAdapter不会使用适配器模式。问题是:是否有一个MouseAdapter实现的模式?

I've been able to find great resources that tell me that MouseAdapter from the Java API doesn't make use of the Adapter pattern. The question is: is there a pattern that MouseAdapter does implement?

我知道它是做什么的:它为MouseListener接口提供了一个具体的类,所以你只需要扩展类以避免实现不必要的模式。

I know what it does: it makes a concrete class for the MouseListener interface so you can just extend the class to avoid implementing unnecessary patterns.

我以为它可能是Bridge模式的一部分。我不确定,因为我不熟悉这种模式。

I was thinking it may be part of the Bridge pattern. I'm not sure though, as I'm not familiar with this pattern.

推荐答案

很好的问题!

我可以看到为什么一个回应者表示Null Object,因为有一些概念上的相似之处。我真的很喜欢这个答案。但是在Null对象中,它实际上是关于消除不断检查null的需要,如:

I can see why one responder stated Null Object as there are some conceptual similarities. I really like that answer. But in Null Object, it really is about removeing the need to continually check for null, as in:

if (obj != null)
    obj.DoSomething();

您可以通过创建一个使用no-op实现覆盖DoSomething()的存根对象来实现。与我的区别在于意图是明确的。如果我看到一个Null对象(在名称中或在docos中),我希望它应该用一个no-op来实现所有的操作。我永远不会期望一个类继承自Null对象。事实上,在我看来,他们应该被密封。

And you do this by creating a stub object that overrides DoSomething() with a no-op implementation. The difference to me is that the intent is definiately different. If I see a Null Object (either in name or in docos) I expect that it should implemenet ALL operations with a no-op. I would never expect it a class to inherit from a Null Object. In fact, in my opinion, they should be sealed.

我不认为适配器是那么坏,因为适配器的意图是适应(更改)不兼容或ackward界面转换为可以消费或使用的格式。这绝对是MouseAdapter的意图。 MouseListener接口确实是ackward,而MouseAdapter将该接口转换成更容易消费的东西。

I don't think Adapter is that bad, as the intent of Adapter is to adapt (change) an incompatible or ackward interface into a format that can be consumed or used. That is definitely the intent of the MouseAdapter. The MouseListener interface is indeed ackward, and MouseAdapter is converting that interface into something more easily consumable.

它适应它是什么?我会说模板方法模式。特别地,它将接口实现方法转换为钩子操作。钩子操作是一种存在于子类中覆盖的方法,通常被实现为无操作,并由基类调用。 (从概念上讲,我猜这是一个Null方法,而不是一个空对象)。它们作为扩展点存在,这就是在这种情况下如何使用它们。

What does it adapt it into? I'd say the Template Method pattern. In particular, it converts interface implementation methods into "hook operations". A hook operation is a method that exists to be overriden in a subclass, is generally implemented as a no-op, and is called by the base class. (Conceptually, I guess it is a Null Method instead of a Null Object). They exists as extension points, and that is how they are used in this case.

这篇关于MouseAdapter:它使用哪种模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:39