Why is MouseAdapter an adapter?

前端 未结 4 460
星月不相逢
星月不相逢 2020-12-10 13:00

The name (and javadocs) imply that MouseAdapter is an adapter (the design pattern). But I don\'t see it as such - it doesn\'t adapt anything to anything, at fir

4条回答
  •  -上瘾入骨i
    2020-12-10 13:29

    As other answers have said, it's not a GoF Adapter pattern. The main purpose of it is to enable one to implement MouseListener (or MouseMotionListener) by over-riding just the desired methods in MouseAdapter (often just mouseClicked()) rather than having to create pointless empty implementations of all the other methods. It therefore saves a lot of unnecessary code, especially when using anonymous event listeners. For example (taken from here)

        someObject.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                ...//Event listener implementation goes here...
            }
        });
    

提交回复
热议问题