Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MouseAdapter: which pattern does it use?

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.

like image 545
varatis Avatar asked Nov 28 '11 21:11

varatis


2 Answers

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.

like image 138
tcarvin Avatar answered Oct 20 '22 17:10

tcarvin


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

like image 45
on2valhalla Avatar answered Oct 20 '22 17:10

on2valhalla