What is the common way to program action listeners?

前端 未结 6 1719
走了就别回头了
走了就别回头了 2021-01-06 01:10

I just started to learn how to use action listeners. To my understanding it works in the following way:

  1. There are some classes which contains \"addActionLis

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-06 01:34

    The way I have always found useful is to create a separate class which implements the ActionListener interface and all the other methods necessary to carry the action. This way, an action is not tied to a specific object and can be triggered from a button, a menu, etc. A little bit like the Command pattern I guess. It keeps the code simple.

    Anonymous classes aren't reusable.

    Redirection to the object containing the listenedObject leads to gigantic classes that are hard to maintain.

提交回复
热议问题