Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design patterns to reduce coupling in Swing application

Hey all, I'm currently working on a Java Swing application and I was looking for some guidence. The application is fairly small, but I'm noticing that as the code base is growing larger, that I have an awful lot of coupling in my object graph. I'm relatively new to Swing, but I've been programming long enough to know where this is headed.

The biggest problem I'm having is setting up my event handling. How should my child windows and objects be communicating events to my higher level objects without having references to them? I've done a fair amount of MVC web coding. Does this pattern lend itself well to Swing? Should I be building my own controller? I guess I'm just fishing for patterns that people have found useful working with Swing.

Thanks in advance for your help.

like image 826
BillMan Avatar asked Jul 28 '10 03:07

BillMan


3 Answers

The best way to reduce coupling in a GUI, I think, is to use an Event Bus. There are several existing implementations out there, including some supporting Swing specifically.

Just google for swing event bus and you'll find.

If you use Guice in your GUI, you may also want to take a look at guts-events.

like image 91
jfpoilpret Avatar answered Nov 18 '22 10:11

jfpoilpret


Yes. MVC is what you have to use. Here is a very good article about MVC and Swing:

http://java.sun.com/products/jfc/tsc/articles/architecture/

like image 24
Eugene Ryzhikov Avatar answered Nov 18 '22 09:11

Eugene Ryzhikov


Another Pattern that might be interesting for you is the MVP (Model View Presenter)-Pattern. This is great for coupling views more loosely to the model. A good explanation by Todd Snyder can be found here.

like image 1
crusam Avatar answered Nov 18 '22 09:11

crusam