Skip to main content

Advice on MVC theory needed

1 reply [Last post]
kg
Offline
Joined: 2007-08-08

Hi,

I'm new to Java desktop development and I'm trying to get up to speed as quickly as possible. With regards to MVC in Java, I have some questions that are theory related, and somewhat difficult to answer with a simple Google search. So I'm hoping someone here might be able to help me get bootstrapped.

So, one question I have is related to various MVC implementations I have seen in the reading I've done so far. In these examples, M,V,C are decoupled, but the controller has a very strong relationship with the view. I don't see a problem with this except I've grown fond of how .NET accommodates this relationship using a slightly different concept of a "Code Behind." Basically what this eludes to is that the View extends from the Controller. All UI members can be declared in the controller as protected allowing access to the view. This seems to encourage encapsulation such that the view may be instantiated by other components without knowledge of the controller.

I'm including an example below for clarify what I mean, but my first question is whether or not this is a familiar technique to Java developers. If so, what is it called and where might I read more about it?

import javax.swing.JFrame;
import javax.swing.JLabel;

public class ClientFrame extends ClientFrameController {

public ClientFrame() {
super("Example");
setSize(300, 400);

_label = new JLabel("my example");
this.add(_label);
}
}

class ClientFrameController extends JFrame {
protected JLabel _label;

public ClientFrameController(String title) {
super(title);
}
}

Second, are there any general desktop tutorials that you might recommend? At this point I'm looking for the less obvious stuff that might make the difference between a functional app and an excellent app.

Thanks a lot!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tarbo
Offline
Joined: 2006-12-18

It's been a while since uni, but I'll try to recall what the professor said back then for Swing in Java 1.4. ;)

In Java, the M-V-C split is more pronounced on separating model from everything else. View and Controller are more-or-less the same --in this context, this means that they are contained in the same class. In Swing, the [i]View[/i] would be how the component is rendered, while the [i]Controller[/i] is the JComponent class, which uses its component hierarchy and listeners to catch and expose events.

In many cases, developers don't need to touch the view; they simply place the components as needed and attach event listeners to influence how the controller works. Slightly more advanced applications may want to provide their own model implementations as well, but much of the controller's inner workings is left untouched.

So yes, I'd say this technique of extending the controller is equally used in Java, since (almost) all components in Swing extend from the base JComponent, which handles the basic controller tasks.

Then again, I rarely make GUIs, so that's the theory as I understand it, and I could be spectacularly mistaken. The people from, for instance, [url=http://forums.java.net/jive/forum.jspa?forumID=73]SwingLabs[/url] or the [url=http://forums.java.net/jive/forum.jspa?forumID=74]Swing & AWT[/url] forum should be able to go into much more detail.

Hope this helps,
Jonathan