MouseAdapter: which pattern does it use?

ぃ、小莉子 提交于 2020-01-14 09:54:07

问题


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?

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.

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


回答1:


Great question!

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();

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.

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.

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.




回答2:


The Null Object pattern should fit the bill. It basically means that an object has methods whose bodies are empty. Both MouseAdapter and WindowAdapter follow this pattern by having no executable code in their methods.

Wikipedia: Null Object pattern




回答3:


i don't think it uses a design pattern. the closest thing might be the strategy design pattern if you look at the direct known subclasses of mouse adapter, but that doesn't quite feel right (to me).



来源:https://stackoverflow.com/questions/8302636/mouseadapter-which-pattern-does-it-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!