fuin.org

Small Open Source Java Tools and Libraries

Optimized View and Logic (Controller) Separation with Swing


Part 3: Utilization of Existing Basic Java Technologies

Fundamentally, the communication between controller and view is nothing more than the sending of (local) messages. For the exchange of messages between loosely coupled components, Java offers a solution called Java Message Service External link (JMS). Instead of re-inventing the wheel, we can simply utilize it for our purposes. David Waldend External link has already authored several interesting Articles External link that cover this topic. He has also developed the SomnifugiJMS External link Framework.
 
Based on our model, the communication landscape would thus look something like this:
 
Communication landscape for JMS/MVC
 
As its AddressController, the AddressListPanel utilizes a special implementation (the ControllerSender), which sends messages to the ControllerReceiver via JMS. The ControllerReceiver - depending on the requested method - calls the actual implementation (AddressControllerImpl), which executes the respective task. The MethodResultSender sends one or several results of the process to the MethodResultReceiver via JMS. The latter calls the respective Listener Method in the AddressListPanel.
 
We have now completely uncoupled controller and view with the assistance of JMS. To this end, the ControllerReceiver is a separate thread, which continuously receives the JMS commands and forwards them to the actual controller for processing.
 
The SomnifugiJMS Framework puts at our disposal a lightweight JMS implementation that has been specifically designed for such purposes.
 
One of the interesting ancillary benefits is that it is indeed possible to switch to any random JMS provider (e.g. Apache ActiveMQ External link) at any time. Consequently, it is possible to implement a controller in a second JVM or on a remote computer (server). Clients in different languages are yet another option. For instance, a viewer outside of Java could listen for respective message and display Swing GUI documents that match the data.
 
Reviewing the above diagram might cause some people to raise the objection that it does not represent the KISS principle. Given the large number of different classes it is certainly not instantly evident that this implementation is any simpler than the SwingWorker approach.
 
However, this conclusion is only partially correct as you will see in the next part.
 
Next Page: Part 4: Simple Utilization for the Developer
 

 
Page: 1 / 2 / 3 / 4 / 5 / 6 / 7