patternjavaMinor
Java MVC Implementation
Viewed 0 times
implementationmvcjava
Problem
I am trying to write an MVC based application from scratch using Java 8 and Swing... I am having some difficulties with this.
I want to know if I am going the correct way about this...
My controller code is:
Views are subclassed from this class:
This is an example of a concrete View:
```
public final class VBasexLoginR extends View {
private JButton connectButton;
private JLabel connectionLabel;
private JTextField hostField;
private JLabel hostLabel;
private JLabel passwordLabel;
private JButton pingButton;
private JTextField portField;
private JLabel portLabel;
private JPasswordField pwField;
private JLabel titleLabel;
private JTextField uidField;
private JLabel userId;
public VBasexLoginR(Controllerv2 c) {
super(c);
I want to know if I am going the correct way about this...
My controller code is:
public class Controllerv2 {
private final Map views = new HashMap<>();
private final Map commands = new HashMap<>();
private final ModelContainer model = new ModelContainer(this);
public void addView(String key, View view) {
views.put(key, view);
}
public View getView(String key) {
return views.get(key);
}
public void showView(String key) {
getView(key).showForm();
}
public void addCommand(String commandString, Command cmd) {
commands.put(commandString, cmd);
}
public void executeControllerCommand(String commandString) {
commands.get(commandString).execute();
}
}
public abstract class View {
protected final JDialog dialog = new JDialog();
protected final GUIAction guiAction = new GUIAction();
protected final Controllerv2 controls;Views are subclassed from this class:
public View(Controllerv2 suppliedController) {
this.controller = suppliedController;
}
// Fires up the form
protected abstract void initComponents();
// Package up the fields into an object of type T and return
public abstract T readFields();
// Self explanatory functions
public void showForm() {dialog.setVisible(true);}
public void hideForm() {dialog.setVisible(false);}
// For fields that need updating
public abstract void updateFields(String[] msg);
}This is an example of a concrete View:
```
public final class VBasexLoginR extends View {
private JButton connectButton;
private JLabel connectionLabel;
private JTextField hostField;
private JLabel hostLabel;
private JLabel passwordLabel;
private JButton pingButton;
private JTextField portField;
private JLabel portLabel;
private JPasswordField pwField;
private JLabel titleLabel;
private JTextField uidField;
private JLabel userId;
public VBasexLoginR(Controllerv2 c) {
super(c);
Solution
I'm only going to scratch the surface here, and leave more seasoned java reviewers take a stab at this one, for I've never written a line of java-8 - but some concepts transcend language barriers.
Naming in particular.
Signatures can, and should talk.
Take this line:
The name
Now there's no ambiguity whatsoever, and
Consistency is King
This is just a minor thing, but I'd prefer consistency in the choice of verbs used to describe an action. For example, if a form is a view and a form gets shown, then a view should be shown too, not opened:
Would be
Same with commands - if a command executes, I wouldn't want the verb run to refer to the same thing, I'd prefer it be consistent:
Would be:
Single-letter identifiers
Don't. This:
Is pretty darn confusing. Does
Anyway, that
Constructor arguments
I like this part -
I'd push it a step further though, and provide a way to initialize the class with models and commands, like this:
And now the client code doesn't need to call
Indentation
Your indentation is a little off, too. Whenever a code file ends like this:
You know something's gone wrong. I'd blame it on the Java-style braces but that's just the c# mug talking, just select all but the signature and last closing brace, and give that Tab button some lovin' ;)
Naming in particular.
Signatures can, and should talk.
public void addView(String viewString, View view) {
views.put(viewString, view);
}
public View getView(String viewString) {
return views.get(viewString);
}Take this line:
public void addView(String viewString, View view) {The name
viewString is pretty confusing. One has to look at how it's implemented to go "oh, that's a key for a hashmap!". How about this instead?public void addView(String key, View view) {Now there's no ambiguity whatsoever, and
getView(String key) becomes crystal-clear.Consistency is King
This is just a minor thing, but I'd prefer consistency in the choice of verbs used to describe an action. For example, if a form is a view and a form gets shown, then a view should be shown too, not opened:
public void openView(String viewString) {
getView(viewString).showForm();
}Would be
public void showView(String viewString) {
getView(viewString).showForm();
}Same with commands - if a command executes, I wouldn't want the verb run to refer to the same thing, I'd prefer it be consistent:
public void runControllerCommand(String commandString) {
commands.get(commandString).execute();
}Would be:
public void executeControllerCommand(String commandString) {
commands.get(commandString).execute();
}Single-letter identifiers
Don't. This:
ModelContainer(Controllerv2 c) {
this.controls = c;
}Is pretty darn confusing. Does
c stand for controller, or for controls? Actually there seems to be something fishy going on here, "controls" is a nice name for a collection of controls, but you have it as ..a Controllerv2 (is there a Controllerv3? Avoid "versioning" classes like this, especially in the early development stages). Notice the puzzled expression in my face? ;)Anyway, that
c would probably be better of as controller.Constructor arguments
I like this part -
final makes your intent very clear:private final Map models = new HashMap<>();
private final Map commands = new HashMap<>();
private final Controllerv2 controls;
ModelContainer(Controllerv2 c) {
this.controls = c;
}I'd push it a step further though, and provide a way to initialize the class with models and commands, like this:
private final Map models = new HashMap<>();
private final Map commands = new HashMap<>();
private final Controllerv2 controller;
ModelContainer(Controllerv2 controller, Map models, Map commands) {
this.controller = controller;
this.commands = commands;
this.models = models;
}And now the client code doesn't need to call
AddCommand 20 times when there's that many commands to add.Indentation
Your indentation is a little off, too. Whenever a code file ends like this:
}
}You know something's gone wrong. I'd blame it on the Java-style braces but that's just the c# mug talking, just select all but the signature and last closing brace, and give that Tab button some lovin' ;)
Code Snippets
public void addView(String viewString, View view) {
views.put(viewString, view);
}
public View getView(String viewString) {
return views.get(viewString);
}public void addView(String viewString, View view) {public void addView(String key, View view) {public void openView(String viewString) {
getView(viewString).showForm();
}public void showView(String viewString) {
getView(viewString).showForm();
}Context
StackExchange Code Review Q#59498, answer score: 4
Revisions (0)
No revisions yet.