• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MVC pattern Java Sample code.

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,
Any sample java codes that implements the MVC pattern please?
Thanks.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Patterns.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
carl varola
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 59
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Frank,
    Thanks alot for your time to develop that,i will study the code and let you know.
    Thanks.
     
    We don't have time for this. We've gotta save the moon! Or check this out:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic