4
\$\begingroup\$

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?

\$\endgroup\$
1
  • \$\begingroup\$ Have you read what this comunity should be good for? It´s for showing own working code, your questin is more suitable for stackoverflow. \$\endgroup\$
    – convert
    Commented Jun 8, 2022 at 11:42

2 Answers 2

3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Singletons, I thought, were only evil if they held state. This one doesn't. Is it that bad? \$\endgroup\$ Commented Apr 9, 2013 at 11:01
0
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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