[Tutor] Webprogramming with the MVC pattern

Alan Gauld alan.gauld at freenet.co.uk
Thu Mar 23 12:46:51 CET 2006


Hi Jan,

> I am about to build my first web application using the MVC
> pattern.

MVC can be done in many ways. Are you trying to build this from
scratch or are you using some kind of Web framework such as Zope
or CherryPy?

> Now I am wondering which might be the best way to initialize
> both a model and a presenter based on the model and its state.

> My controller should look like this:

I assume from the code that you want to use a single controller
for all of your models not a controller per model? This requires
that the controller be model agnostic and cannot know about
the classes it controls?

#########
model = Models.ModelFactory(cgi_parameters)
view = Views.ViewFactory(model)
view.Display()
#########

One possibility is to use a registration scheme whereby the
controller keeps a list of mopdels and correspomnding views.
When a new model or view is defined the class is registered
with the controller. Then it becomes the controllers responsibility
to know what kind of model is required for a given view.

> The factory method ModelFactory would check the parameters
> and return an appropriate model:

This is the alternative approach where the Factory objects must
dynamically determine what kind of model or view is required.
It usually involves a long if/else chain that must be updated as
new classes are added, or else it uses a registration scheme of
its own whereby the classes are kept in a dictionary keyed by
class name. I favour the latter approach since it doesn't require
modification of the factory when a new class is added.

I'd therefore make the ModelFactory a class and then I'd
remove the valid_models line below and add a class method
to register a new model, then call that method after each
model definition. (You can in fact create a single Factory
class which is then either subclassed for models and views
or alternatively instantiated twice)

########
def ModelFactory(params):
        valid_models = dict(location=Location, prices=Prices, 
booking=Booking)
        return valid_models[params['type']](**params)

class Location:
    ...
class Prices:
    ...
class Booking:
    ...
########

> But I am stuck with the ViewFactory method.
> Since the appropriate view depends on both the
> model's class (type) and state,

I'm not sure why the view would depend on model state,
it shouldn't in MVC! Normally the user selects the view
they want and the controller connects it to an appropriate
model. You should be able to support multiple views
of the same model at the same time.

> I am not sure how to implement this. I could do it the ugly way:

########
def ViewFactory(model):
    if (model.__class__ == 'Booking' and model.state == 'new'):
        view = new NewBooking()
    elsif (model.__class__ == 'Booking' and model.state == 'review'):
        view = new BookingReview()
    ...
    return view

class LocationView:
    ...

> But there must be a better way. Could you give me a hint?

You can use a registration/dictionary scheme as above. If the
controller understands the relationship of view to model then
when the view is created the controller joins the two together.
In your case you seem to assume that the model is controlling
which view is presented which is not an MVC paradigm with
which I'm familiar so I'm not sure how that would work..
But you can still avoid the if/else chain by using a tuple of
model/state to access the view dictionary. You just need to
register the view with all combinations of model/state that
could trigger the view...

Alan G.



More information about the Tutor mailing list