This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes MVC pattern Java Sample code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "MVC pattern Java Sample code." Watch "MVC pattern Java Sample code." New topic
Author

MVC pattern Java Sample code.

carl varola
Ranch Hand

Joined: May 16, 2002
Posts: 59
All,
Any sample java codes that implements the MVC pattern please?
Thanks.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Moving to Patterns.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Something like the following is probably the simplest example:

However, as it stands, that's not very useful. The whole point of patterns is that they are not just pre-built code libraries. So to give a sensible code example for you, we really need to know a bit more about your application.
Can you tell us a bit more about what you need ?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
carl varola
Ranch Hand

Joined: May 16, 2002
Posts: 59
Frank,
Thanks for the sample code.
All what I need is to show my students a working sample code to demonstrate MVC pattern.
I cannot see complete decoupling between those 3 layers in the sample code,would you please modified that codes in a way where I can see MVC better?
Usually the start point is the view...right?I mean I will put main() method there?
We have lots of discussing about MVC and seems to me we cannot see it very well,please any possible clear explanation?
Thanks.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Well I'm still waiting to learn a bit more about your application. All you have told me is that you want to start it from "main". This tends to imply that it's not a servlet or applet based example you are looking for, as they don't have a "main". Please tell us more about what context you are thinking of applying this pattern in.
In the meanwhile, if you are more concerned about decoupling, consider this slightly more elaborate example:

I've tested it so I know it will compile. Save the code block as "Application.java", Compile it with "javac Application.java", and run it with "java Application".
At the prompt, you can type one of the following commands:
  • X :- to exit the calculator
  • A :- to add a new view. The controller can have as many views as you like.
  • R followed by a number :- to remove the numbered view
  • + followed by a number :- to add the number to the stored value
  • - followed by a number :- to subtract the number from the stored value
  • = to refresh all the views


  • try the following sequence, as an example

    Does this offer any more help in understanding how MVC can help with decoupling?
    carl varola
    Ranch Hand

    Joined: May 16, 2002
    Posts: 59
    Frank,
    Thanks alot for your time to develop that,i will study the code and let you know.
    Thanks.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: MVC pattern Java Sample code.
     
    Similar Threads
    suggest best site fro uml
    MVC pattern Java Sample code.
    MVC + Observer pattern
    GUI�s, OO, events and decoupling
    data reflection using mvc with valueof object pattern