views:

730

answers:

4

I've been told Java is not the greatest pick to follow an MVC architecture. I believe I've seen some Java framework solutions to ease this roadbump. However, I a bit confused on why this is. More specifically, why Java's attempt at MVC is often mocked as a "wannabe" approach. I come from a ObjC background (w/ Cocoa of course) and would love to hear from the seasoned programmers about why MVC with Java is said to fall short.

Appreciate it!

+9  A: 

You say you come from a Cocoa background so I'm assuming you're not talking about web-framework MVC. Most webframeworks that say they do MVC are actually using a different pattern called Front Controller, it's a bit of a different story from normal MVC because you need quite a lot of plumbing to make that work. Hence all the MVC frameworks in the web-world, originally MVC was a pattern for window-based applications. For these applications you can make MVC work without needing a framework.

MVC is a pattern that can be implemented in any object oriented language. There's no reason why you can't do it in Java. The only reason I can think of why people would call MVC on Java a "wannabe" approach is that the UI libraries on Java like Swing do not actually require you to do MVC. They just give you the tools to implement a View. You'll have to create controller and model classes yourself.

Discussions about what 'real MVC' is are usually not really constructive anyway. MVC is a pattern developed for smaltalk about 30 years ago. None of the implementations that are used now are exactly the same anymore, many implementations are improvements. You've got different flavours called MVP, MVVM, Document-View etc. And they are all usefull in some cases. The important thing to take away from this is that its a good idea to separate your UI logic from your application logic. Java can do this and so can most other languages. People claiming otherwise are probably just afraid to look outside the language they're comfortable with.

Mendelt
Java's UI frameworks are AWT and Swing. Spring is something different entirely.
Martin OConnor
Oops. I meant swing, not spring. Fixed it, thanks for noticing!
Mendelt
+4  A: 

At it's roots, MVC is a programming model. I consider it to be platform and language agnostic. I used to follow the MVC principles in C++, so I'm not sure why any language would be considered as "falling short" with regard to following these principles. Admittedly, I'm a .NET person these days, but I think with a little effort they could be applied to Java. See Java Model-View Controller for some ideas.

billb
great link. ;-)
djangofan
+2  A: 

I feel that the MVC pattern as a framework for dynamic, data driving web applications does seems to require less verbosity and hence less code with dynamic languages like Python and Ruby, which is a good thing.

Dynamic languages make it easy to avoid the dependency between the M, V and C which is one of the purposes of the pattern. For example in Python you can just pass your model objects to the view without the view requiring a dependency on the model type, it just cares that any type with the same attributes is passed in. Once you start using a templating language in the views, like in Django, it doesn't even cause script errors when an attribute doesn't exist.

To avoid these dependency in a strongly typed language, you often start passing passing around dictionaries with literal strings as keys, meaning most of the benefits of strongly type languages are lost (ie. your compiler won't tell you named a key "person" and then attempt to find it with a key "persons", this will be discovered at run-time)

Also, given the MVC pattern lends itself to testing by allowing the M, V and C to be tested individually, there is less advantage using strongly typed compiled language as tests can be used to validate your application is working as it should be.

tarn
i think the question is about MVC, the modular GUI programming style, and not MVC, the layered web architecture. totally different things
Javier
Yes, it appears so. Note to self, read questions more carefully.
tarn
Still they're not *totally* different things, it is still the same pattern. I think this answer is still valid as I feel it does add value to the discussion and I do specify I am talking about MVC in web implementations.
tarn
It was, but I understood where you were coming and nevertheless, do appreciate it!
Rev316
+4  A: 

would love to hear from the seasoned programmers about why MVC with Java is said to fall short.

It doesn't. There is absolutely nothing wrong with implementing MVC in just about any language. It's a pattern, not a language feature. Also, I've never heard anyone say "MVC in Java falls short" before - my guess is that either the quote was taken out of context or the person who made the statement has little experience with Java or intended something else.

You don't have to use the MVC-related classes in Cocoa just to write a Cocoa application. It's nice that they're there, and that there's all kinds of great support in the tools (like Interface Builder) to use them, and there are probably very few cases where you wouldn't use them, but there's nothing fundamental to the Objective-C language that makes an MVC implementation "better" than an MVC implementation that was coded in Java. Furthermore, just as the Cocoa frameworks provide all kinds of great support for MVC with the various Controller and View classes, there are scores of good frameworks for Java which provide support for implementing MVC.

erikprice