tags:

views:

431

answers:

3

I have some domain logic implemented in a number of POJOs. I want to write a Swing user interface to allow the user to initiate and see the results of various domain actions.

What's the best pattern/framework/library for communications between the UI and the domain? This boils down into:

  • the UI being able to convert a user gesture into a domain action
  • the domain being able to send state/result information back to the UI for display purposes

I'm aware of MVC as a broad concept and have fiddled with the Observer pattern (whose Java implementation has some drawbacks if I understand correctly), but I'm wondering if there's an accepted best practise for this problem?

A: 

I have used the Observer pattern (using AspectJ magic) in the past with some success, but found that unless you were careful it quickly became a cluster.. uhh.. flick?

It quickly became hard to manage and most importantly extremely hard to debug.

Edit:

To expand slightly on my answer, we were using SWT, not Swing, so YMMV. We basically used AspectJ to hook up the transference of data from the UI components to the model objects. These model objects were dumb POJOs.

Actual business logic was done by 'watching' the model objects with AspectJ and firing off the required event if they changed. So if you changed a value in a textbox AspectJ would fire and copy that value into a POJO. If that field in the POJO had an event on it for business logic that would then fire. If that logic modified any POJOs (and it could) AspectJ would notice and copy the value from the POJO into the UI component.

SCdF
I think you're confusing POJOs (http://en.wikipedia.org/wiki/POJO) with JavaBeans. The latter are dumb data stores, but the former may indeed be rich domain types with the full range of behaviour/logic. The "plain" bit of POJO simply indicates no dependency on any specific framework or technology.
Andrew Swan
Yeah, my bad. Updated post to reflect :)
SCdF
+1  A: 

Definitely MVC - something like this example which clearly splits things out. The problem with the Swing examples is that they seem to show the MVC all working within the swing stuff, which does not seem right to me

Chris Kimpton
That article looks really well written; I'll check it out and see how I go.
Andrew Swan
A: 

MVC is fantastic for an individual widget, however it gets a little unruly when you have "pages" and "forms" with lots of widgets.

One thing that might be worth looking into (and I'm not endorsing it, I haven't actually used it, just implemented something very similar for myself) is the Beans Binding Framework (JSR295)

Aidos