Search code examples
javauser-interfacedesign-patternsclassmediator

Does Mediator Pattern work in this situation?


So for my current project, there are basically three main Java classes:

  1. GUI
  2. Instant Messaging
  3. Computation

Essentially, there needs to be full communication, so we've decided to use the mediator approach rather than than allow the GUI to run the entire project.

Basically, the mediator is going to encapsulate the communication. The problem we've run into is how to allow the GUI components to update without building a ton of methods for the mediator to call anytime something completes.

Ex. Say the GUI wants to log in the user, it goes through the mediator to create a thread and log in, but then the mediator has to relay the success/failure back to GUI as well as update a status message.

The other issue is things that need to update the GUI but do not need the moderator. Is it practical to just allow the GUI to create an instance of that class and run it or should everything go through the mediator?

Our original design just had the GUI managing everything, but it really killed reusability. Is there a better design method to use in this case?


Solution

  • If you're finding Observer to bring too much overhead, Mediator may be the best way to go. I definitely think that you shouldn't have the GUI run the show. If you're going to use the Mediator pattern, the mediator itself should be in charge. Something you might consider is a variant of the Command pattern. If you were using Ruby, I might recommend passing function callbacks around as a means of avoiding having the mediator contact the GUI for every little thing. But since it's Java, some form of encapsulating an action in Command pattern style may help.