patternjavaMinor
MVC Controller in Java Swing Apps - Singleton or public static
Viewed 0 times
mvcpublicjavacontrollerswingsingletonappsstatic
Problem
This article has left me a little confused.
In explaining a method on how to design MVC into Java Swing apps the author defines the controller class as Singleton and relays a series of calls to the Controller for each operation.
In a view object:
And inside the controller class:
I don't understand why this has been made so complex! Wouldn't it be simpler, less error prone and more modular to simply:
and
Is anyone familiar with this design pattern? Is there reason to this method?
In explaining a method on how to design MVC into Java Swing apps the author defines the controller class as Singleton and relays a series of calls to the Controller for each operation.
In a view object:
...
Controller.getInstance().setOwner(this);
Controller.getInstance().setNameTextField(this.nameTextField);
Controller.getInstance().setEmailTextField(this.emailTextField);
Controller.getInstance().processForm();
...And inside the controller class:
public void processForm() {
result1 = doSomethingWithName(this.nameTextField.getText());
result2 = doSomethingWithEmail(this.emailTextField.getText());
this.owner.setResult(result1, result2);
}I don't understand why this has been made so complex! Wouldn't it be simpler, less error prone and more modular to simply:
Controller.processForm(this, this.nameTextField.getText(), this.emailTextField.getText());and
public static void processForm(OwnerClass owner, String name, String email) {
result1 = doSomethingWithName(name);
result2 = doSomethingWithEmail(email);
this.owner.setResult(result1, result2);
}Is anyone familiar with this design pattern? Is there reason to this method?
Solution
It's been a long time ago since I last used an MVC approach in Swing, but I immediately notice some things I wouldn't do.
So yeah, it seems I would agree with you. Perhaps try looking for other MVC examples with Swing. This article doesn't look particularly appealing IMHO.
- Don't use a singleton, singletons are evil!
- Passing view components to the controller? When I do MVC, I tend to pass the controller to my view elements, preferably just an interface. The view can request actions on the controller. This allows for total decoupling of the view. So you can also switch to something else than Swing. This probably isn't the 'default' approach of MVC with Swing however.
So yeah, it seems I would agree with you. Perhaps try looking for other MVC examples with Swing. This article doesn't look particularly appealing IMHO.
Context
StackExchange Code Review Q#1212, answer score: 3
Revisions (0)
No revisions yet.