views:

74

answers:

1

Maybe it's not worth worrying about in this scenario, but lets say you have two classes, a JFrame with all its components, and a server-like class that handles requests from remote clients. The user is able to start and stop server objects through the GUI, and is shown various events that happen to each server object. Whether or not I use an explicit pattern (like MVC), it seems like the JFrame needs a reference to the server class (to call start and stop) and the server needs a reference to the JFrame (to notify of it of certain events).

Is this a problem, or am I looking at this situation in the wrong way?

+4  A: 

This sounds like a place to apply the Listener pattern. Your server could have a method called addSomethingListener(SomethingListener listener), which the JFrame calls with an implementation of SomethingListener. Your server then would call the listener's methods whenever appropriate events happen.

Will
I agree that the Listener pattern is the way to go, but just because the reference to the JFrame is in an ArrayList and isn't hard coded doesn't remove the fact that there's a circular reference. Actually, on second thought, since circular references aren't a problem (GC can hanndle them), and the circular dependency is really the only problem. The Listener pattern solves this because it's fine for the GUI to depend on 'business' code, but not the other way around. Thanks for helping me reason that out.
drhorrible