Sign in to follow this  
Silent Dragon

Using MVC design pattern

Recommended Posts

Silent Dragon    184
I am currently writing a piece of software that uses the MVC pattern. I have a class, which can be called 'Element' for the sake of this discussion that holds data, so would be the 'model' part of MVC. I also have a class which display's the data (the view), but the Element can be selected by the user by clicking on the view (which is obviously handled by the controller), but which part of the system should store the boolean that designates the fact that a particular element has been selected? It seems that the model would be the easiest place to add this boolean - but it seems the wrong place to put it, because it isn't really part of the data, it's more part of the control of the view. The view could also store a list of selected elements, but I'm not sure if this would be the 'correct' placing either. Any thoughts? -SD

Share this post


Link to post
Share on other sites
Antheus    2409
Model.

Quote:
it isn't really part of the data

It is part of data model, not data. Data is stored elsewhere.

What you have is selectable data model. A DumbView would just display the data, SelectableView would also be able to display selected elements. Same for controller.

The important distinction here is that model is not data. It is merely some functional representation of data in single place, but the data is physically stored externally in some other form. Perhaps a database or multiple files. This allows you to tack additional functionality on the model itself, while the model interface remains consistent.


Personally I've always found rigid MVC to be too artificial. It works good on paper, but ends up hackish in real world. The view:model separation does not offer ideal separation between when a view and what a model are. Selection is just one of such concepts which transcends such trivial separation, especially if more complex gestures are required as is often the case with rich interfaces. Trying to apply MVC to media rich content becomes quite challenging since some performance issues also crop up due to artificial separation which prevents more cohesive designs.

There's also this new MVVM concept which tries to address some of these shortcomings.

Share this post


Link to post
Share on other sites
Silent Dragon    184
Thanks Antheus. My understanding of MVC seems to have been a little off, so thanks for pointing out that distinction.

MVVM seems interesting, but due to the fact I'm writing in Java I already have MVC set up in the form of Observable and Observer classes, so it's not worth looking into for this project. Thanks for pointing that out though, I hadn't heard of it before.

-SD

Share this post


Link to post
Share on other sites
Ocelot    375
You can use SelectedModel model which keeps your selected model, and has its own view (eg. box to indicate selected object if it is visual view)

Share this post


Link to post
Share on other sites
Nypyren    12074
MVVM sounds like it takes this:

Model -> View

and turns it into this:

Model -> Decorator -> View

Where Microsoft calls the Decorator the "ViewModel".


This is exactly what I was thinking when you said you wanted to separate your 'selected' bool from your model.

Now that I think about it, I've actually used MVVM a lot but just didn't know it had a name.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this