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.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to