Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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:

...    
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?

share|improve this question
add comment

2 Answers

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.

  1. Don't use a singleton, singletons are evil!
  2. 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.

share|improve this answer
    
Singletons, I thought, were only evil if they held state. This one doesn't. Is it that bad? –  Pureferret Apr 9 '13 at 11:01
add comment

I agree with steven concerning Singletons. I think the most convenient way to call something like this is a builder-pattern-like approach ("fluent interfaces") similar to StringBuilder:

Controller.getInstance().setOwner(this)
      .setNameTextField(this.nameTextField);
      .setEmailTextField(this.emailTextField);
      .processForm();

This is Java's closest approximation of named and default arguments in other languages (e.g. you don't have to remember the order of parameters, you can make some of them optional, and with a little type trickery you can ensure that processForm can be only called if all mandatory args are set).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.