4

The usage of Adapter patttern in the java.awt.event package looks confusing to me. On the first hand it seems a clear violation of Interface Segregation Principle ( ISP ).

Like the MouseMotionAdapter class implements MouseMotionListener but provide 'NIL' implementation for both the overridden methods.

This is precisely what ISP violation is all about ? Going by ISP, MouseMotionListener would be rather split into two separate interfaces, one each for moseDragged and moveMoved behaviour ?

Perhaps splitting interfaces in this way would spiral the number of interfaces and also would make the coding more inelegant as each implementing classes would require to implement large number of interfaces.

Just need some clarification if my arguments are justified ?

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
aknon
  • 1,408
  • 3
  • 18
  • 29
  • This is precisely what ISP violation is all about? - Yes. But the ISP problem is not always solvable unless you have never more than 1 method per interface which will at some point cause ugly code which is what all those principles try to prevent. – zapl Dec 13 '13 at 10:39
  • So strictly speaking ISP should not be advocated as a 'principle' as even the use of ISP ( under some cicumstances, as already noted ) may lead to a unreadable and difficult-to-manage code – aknon Dec 13 '13 at 11:09
  • 2
    Principle != Law. OO design is a complex optimization problem that has probably no optimal solution. Especially when you don't know every use case you'll ever have in the future during the design. There will be violations to all principles somewhere and your goal is to minimize them as a whole. Providing (`..Adapter`) classes that you can extend with just the methods you actually needs also "removes" the ISP problem in your code. You don't depend / depend less on methods that you don't need. – zapl Dec 13 '13 at 13:32
  • @zapl: I think your comment could be converted to an answer. That seems like a totally reasonable explanation to me. – LordOfThePigs Jan 06 '14 at 14:35
  • `XyzAdapter`'s in AWT are only stubs, not the implemention of adapter – Yousha Aleayoub Nov 22 '18 at 11:16

1 Answers1

0

It is not a violation of the ISP.

Short version: You are only thinking about the part of the system where you write your handling code. You are not thinking about where the events are generated (all in the same place, the mouse monitoring code), nor are you thinking about the code for registering for these events.

Long version: Think about how MouseEvents are probably generated. Java has a class that monitors mouse activity, ultimately at a native level, and generates events accordingly. All the MouseListeners are in a listener multicaster chain. If you want to listen to multiple kinds of mouse events, you have to do a separate registration call for your listener to every kind of mouse event. You have to be a part of every sort of multicaster chain. And then, all the classes that generate these MouseEvents have to ensure that the events are getting relayed to the proper listener event type, which means you have to have a new event Queue for every type of event - unless you want to have big instanceof chains.

Or, alternately, you could just unify them into one MouseListener interface, and have your class do nothing when an event occurs that you don't care about. This adds a tiny bit of extra code if you can't extend the MouseAdapter class, or nothing at all if you can. The gains in the rest of the system more than outweigh those few extra { } methods.

durron597
  • 31,968
  • 17
  • 99
  • 158
  • 1
    Yes you are right. I guess when applying ISP one must definitely ask oneself as to what are the `interfaces that should be segregated ?` If the interface methods are `likely to be used together, they must be kept together`. With this it becomes rather easy to understand that the different mouse events should be clubbed as the same interface, instead of segregating. – aknon Apr 02 '14 at 06:43