HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Swing MVC and Command Pattern

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
mvcswingandcommandpattern

Problem

I am writing an application with Java 8 using NetBeans, and want some reassurance that I am going the correct way with this.

I am finding it tough because of the use of Swing.

I have gone for one model (for now), one controller and a set of views. All of the MVC examples for Java/Swing involve one JDialog, one model and one controller.

The controller maintains a list of views and commands, each of which are in a HashMap and are initiated in the main method of the application.

Each command takes the dependencies required to carry out the logic between the view and model in it's constructor. I know this is not the strict implementation of the Command pattern, but I can not think of how to get the two talking to each other without violating the Open/Closed principle.

Also, because of the nature of each View, in each command, I have made reference to the concrete version of the view instead of the abstract version -- this is for updating various fields in each view.

Please criticize my code... I am new to this, and need all the help I can get.

Below is what I have at the moment:

TopLevelController.java:

```
public class TopLevelController implements ActionListener {
private final Map views = new HashMap<>();
private final Map commands = new HashMap<>();
private VFrontOfHouse frontOfHouse;

private Model model;

public void initiate(VFrontOfHouse frontOfHouse) {
// Have a reference to the top level container (Front of house)
this.frontOfHouse = frontOfHouse;

// Iterate through the views, and initiate them with this controller as a listener
Set keys = views.keySet();

for(String viewKey : keys) {
views.get(viewKey).setListeners(this);
System.out.println(viewKey);
}
}

public void setModel(Model model) {
this.model = model;
}

public void addView(String viewKey, View view) {
views.put(viewKey, view);
}

public View getView(String viewKey) {
View view = null;

try{
view = views.get(viewKey);
}

Solution

Just a few comments, not really relevant to your main concerns. Swing is strange, and I myself get often lost in its logic.

try{
    view = views.get(viewKey);
}
catch(NullPointerException ex) {
    JOptionPane.showMessageDialog(frontOfHouse, "No such view: "+viewKey);
}


There's no NPE thrown here. You simply get and return null. Anyway, catching exception instead of proper checks is wrong. It should read

public View getView(String viewKey) {
    View view = views.get(viewKey);
    if (view==null) {
        JOptionPane.showMessageDialog(frontOfHouse, "No such view: "+viewKey);
    }       
    return view;
}


And it's quite possible that this is not the right place for showing any messages.

// TODO code application logic here


Is there something missing?

// handle exception


Do it.... in this case some logging should be enough. Or maybe a comment like // ignored as setLookAndFeel is rather unimportant.

// Set System L&F
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());


A nice example of an anti-comment stating the obvious. Do you know anyone who could be helper by this???

// Create the controller
final TopLevelController controller = new TopLevelController();


Again.

// Create the front of house view
final VFrontOfHouse frontOfHouse = new VFrontOfHouse();


And again.

// Create the model
 Model model = new Model();


And again.

Your main is pretty long. I always try to keep it at <5 lines as the static context is too bad. Create a helper class containing

private final TopLevelController controller = new TopLevelController();
private final VFrontOfHouse frontOfHouse = new VFrontOfHouse();
private final Model model = new Model();


I must admit that all the Swing MVC and command pattern don't work for me well. Instead, I prefer putting the application together using Guice and EventBus (my listeners just post the event to an object which gets everything it needs in its constructor).

Code Snippets

try{
    view = views.get(viewKey);
}
catch(NullPointerException ex) {
    JOptionPane.showMessageDialog(frontOfHouse, "No such view: "+viewKey);
}
public View getView(String viewKey) {
    View view = views.get(viewKey);
    if (view==null) {
        JOptionPane.showMessageDialog(frontOfHouse, "No such view: "+viewKey);
    }       
    return view;
}
// TODO code application logic here
// handle exception
// Set System L&F
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Context

StackExchange Code Review Q#60234, answer score: 2

Revisions (0)

No revisions yet.