3

I would like to ask whether from a design pattern point of view whether it would be better to place listeners for GUI within the "view" or the "controller". A colleague believes that the "view" is the most natural place, but i'm not so sure.

ThomasW
  • 16,981
  • 4
  • 79
  • 106
Kay
  • 845
  • 7
  • 21
  • 33

1 Answers1

2

If you are talking about Swing then, as previously discussed, MVC in Java is not a clear and simple as the pattern suggests. To answer your question, then, depends on how you define "view" and "controller" with respect to a specific application, and what you mean by "placing listeners" in one or the other.

I take the view the listeners are part of the controller mechanism - they provide a loose(ish) coupling between the view (that displays the current state) and the model (that maintains the current state), and provide a way for the two to interact. However, most Swing listeners are very tightly bound to UI events - mouse buttons being clicked, items being selected from lists, etc. - and so you may want to create an additional layer of abstraction which takes these UI events, which are captured by listeners, and translates them into something more general to the domain of your application. An EJB, for example, can provide a common interface for some business logic which may be triggered by a Swing UI or an API call via a web service. The controller, then, is the EJB, and the Swing event listener that triggers a call to that EJB is in the view.

Community
  • 1
  • 1
Stewart Murrie
  • 1,319
  • 7
  • 12