0

In a desktop application, I am developing a model for let's say, cities.

class City(object):

    def __init__(self, name, population):
        self._name = name
        self._population = population

and I want to implement a edit method to change its private properties. The idea is to open a window with text entry fields so that the user can write new values. In order to make it testable and decoupled, I have done like this:

# Within City class
def edit(self, get_properties_function=None):
    """
    Edit properties. Don't pass get_properties_function except for testing
    """

    if get_properties_function is None:
        get_properties_function = self._get_city_properties

    new_name, new_population = get_new_properties()

    self._name = new_name
    self._population = new_population

def _get_city_properties(self):

    # launch GUI and get the new values

    return new_name, new_population

Now the questions:

  • The dialog that asks for information, is a view, right? which leads me to the next question
  • If I have a view, I think I should go further and consider implementing a controller. So, How to implement MVC here?

My idea is to have three classes (M-V-C) and each time that I instance my "city concept", I instance the Model but the View and the Controller as well. And the Controller becomes the public interface of a "City" This sounds like overkill, over-complicated.

I feel I have misunderstood the real MVC pattern because of web programming.

bgusach
  • 14,527
  • 14
  • 51
  • 68
  • 1
    Model is a layer, not a class. In your example the `City` would actually be a [domain object](http://c2.com/cgi/wiki?DomainObject), which is part of model layer, but **is not model**. As for when to use it .. the goal of MVC design pattern is to add additional constraints to an object oriented code, for situations when free form OO code becomes to large to manage (over 20k lines, at least, IMHO). – tereško Mar 14 '13 at 19:18
  • It's very confusing the MVC pattern. Most of people don't understand it properly and use PAC pattern without knowing it. So what do we understand by the concept "model"? are domain objects not models of elements? Thanks for the answer. – bgusach Mar 14 '13 at 19:40
  • I have written a longish [answer](http://stackoverflow.com/a/5864000/727208) on the subject .. assuming you can handle being exposed to php code in examples. – tereško Mar 14 '13 at 19:43
  • Although I still don't understand MVC and how to do this, you've opened my eyes: the model is not a class. This misunderstanding comes from working with php MVC frameworks (which are not MVC at all), where models are classes. Thanks for the link, probably one of the best answers I've ever read. – bgusach Mar 14 '13 at 21:37
  • A small note: the misconception actually originates from RubyOnRails. It was originally intended as a prototyping framework and simply used same names for things as MVC pattern for the sake of familiarity. The problems begun when people started to use it for other then prototyping. The extreme popularity gave rise to various clones in other languages (basically every web framework that was originally released in 2005-2008 time period). – tereško Mar 14 '13 at 22:01
  • Model-View-Controller can be separated programs, it is the blind point. – Louiszen Yip Jan 21 '20 at 04:05

1 Answers1

-1

----- update ----
As tereško said Controller not read from Model. The relationship defined wikipedia here http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

----- original ---
Your class City is model and the dialog is view so what is the controller.

I think you need another class to handle the "model editing".

All you dialog's data offered by the controller and keep away with class city.

I try to describe my opinion by a data flow.

Startup, show the window(dialog?)
View -> (get data) Controller -> (read properties) Model
View <- Controller <- Model (return data)

You want modify the data
View -> (send modified data to) Controller -> (update model data) -> Model
View <- Controller <- Model (Your dialog display updated)

Pikaurd
  • 1,114
  • 1
  • 8
  • 22
  • Does it mean that a "city" needs a unit of Model View and Controller? If so, if you want to instance a "city", what do you instance? the controller or the model? And, when using full MVC, is the controller the "public interface" of a city? If I need to retrieve a property of the model, I have to call the controller to call the model? – bgusach Mar 14 '13 at 12:00
  • In MVC design pattern the **controllers do not read from model layer**. – tereško Mar 14 '13 at 19:23