MVC and individual elements of the model under a common base class
Posted
by
Stewart
on Programmers
See other posts from Programmers
or by Stewart
Published on 2011-02-27T18:49:01Z
Indexed on
2011/02/27
23:32 UTC
Read the original article
Hit count: 301
mvc
Admittedly my experience of using the MVC pattern is limited. It might be argued that I don't really separate the V from the C, though I keep the M separate from the VC to the extent I can manage.
I'm considering the scenario in which the application's model includes a number of elements that have a common base class. For example, enemy characters in a video game, or shape types in a vector graphics app. The view wants to render these elements. Of course, the different subclasses call for different rendering.
The problem is that the elements are part of the model. Rendering them is conceptually part of the view. But how they are to be rendered depends on parameters of both:
- Attributes and state of the element are parameters of the model
- User settings are parameters of the view - and to support multiple platforms and/or view modes, different views may be used
What's your preferred way of dealing with this?
- Put the rendering code in the model classes, passing in any view parameters?
- Put the rendering code in the view, using a switch or similar to select the right rendering for the model element type?
- Have some intermediate classes as a model-view interface, of which the model will create objects on demand and the view will then render them?
- Something else?
© Programmers or respective owner