• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

MVC. How to prohibit view model writes

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am writing MVC editor application. I put model related classes into single package. Most items have package visibility. I hoped to restrict access to modifying methods to my views. Only controllers can modify data. But I write Save operation and, you'll laugh, but controller needs to modify data and it's impossible for controller too. The first idea was I need something like class friends (C++), but there are no Java. Also that would couple model with controller implementation permanently.
The question is: do you hide/restrict model modifications to Views? If you do so, how do you implement it? Is it worth doing at all?

Regards
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not quite sure what is the real problem. Aren't you (your team) the developers of the application? Why should you create a read only model?

./pope
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the Interface Segregation Principle. Don't let the View know about the actual Model classes, but only about interfaces they implement. Don't add methods to those interfaces that let the View change the Model's state.

Does that help?
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Interface Segregation Principle



Ilja from did this come from?

./pope
 
author & internet detective
Posts: 42134
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Interface Segregation Principle

[edited to fix link]
[ November 16, 2004: Message edited by: Jeanne Boyarsky ]
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeanne. I had already found the link and quickly passed throught the article. From time to time I just got a little rusty accepting all this very well packed namings .

./pope
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody! Ilja, this is 100% great solution

For another part of the question: is it worth it? I'd say it is. It is so easy to take a shortcut and modify model from view. Still everytime I do that I am just creating another problem. This read-only interface approach is easy to implement. I'd vote for it. It's a normal data encapsulation. Would you?

Regards
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found it interesting. Before voting for it I would ask myself: how large is model, am I in control of the development (I am not to write down a few lines as a programming guide).

./pope
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found it quite useful for my project. I have an object in model where presentation layer should now only name and value (the object itself is much more complex). It's better to write such an interface. Also this way I can reuse my view and it's hard to modify model. Of course if model object will implement this interface then view could cast reference and still access object. I think probably next step would be create separate class which will provide read only interface to view, but it's a bit overhead. I will settle for middle, interface.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vladas Razas:
It's better to write such an interface. Also this way I can reuse my view and it's hard to modify model.



Yes, that's one of the benefits.

Another one, especially important in bigger projects: it reduces compile times. The View now only needs to be recompiled when the small interface changes, not every time the implementing class changes!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic