[Tutor] Webprogramming with the MVC pattern

Jan Eden lists at janeden.org
Thu Mar 23 10:27:55 CET 2006


Hi,

I am about to build my first web application using the MVC pattern. 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:

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

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

########
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 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:
    ...
class NewBooking:
    ...
class BookingReview:
    ...
class BookingConfirmation:
    ...
########

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

Thanks,

Jan
-- 
A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.


More information about the Tutor mailing list