Relationship between GUI and logic?

David C. Ullrich dullrich at sprynet.com
Fri May 23 11:01:49 EDT 2008


On Fri, 23 May 2008 09:13:50 -0400, "John Salerno"
<johnjsal at NOSPAMgmail.com> wrote:

>"Bruno Desthuilliers" <bruno.42.desthuilliers at websiteburo.invalid> wrote in 
>message news:4836747b$0$15488$426a74cc at news.free.fr...
>> Ever heard of the "Model/View/Controller" pattern ?
>
>Yes, I have, but I probably don't understand it well enough yet. For 
>example, I don't  really know what is meant by phrases like "build a model", 
>"the view registers itself with the model", "interations are sent to the 
>appropriate controller" -- I may understand them literally, but I get the 
>feeling that the implementation of these ideas are beyond me. 

I doubt that.

I've done things like this. I ended up with just a Model and a View,
no Controller. I assumed that meant I was being Bad - I read once
that that often happens, specifically with GUI programs.

An example, certainly not meant to be definitive, maybe not
even right, probably not "right", but illustrating explicitly
what those terms you claim not to understand might mean
in an implementation:

class Model:
  """Encapsulates an abstract version of the game:
Knows nothing about how the players make moves,
the moves are reported to the model via the 
ProcessMove method. Knows nothing about how the
state of the game is displayed to the players;
that's handled by the view or views. The model
might contain a matrix, where the entry in each
cell is None or a certain Piece object. The model
knows that if a certain Cell contains a King then
it's legal to move that King to this Cell but
not to that Cell - that's part of the rules of the
game, something the View knows nothing about"""

  def __init__(self):
    #set up internal state, for example
    #self.board = [[None]*8]*8
    #and then self.board[0,4] = Queen[WhitePlayer],
    #etc. Whatever...

    self.views = []

    #or just self.view = None if there's
    #no reason for multiple views. When I did
    #something like this once I decided at the
    #end that I should have allowed multiple
    #views even though there was only one to start,
    #because functionality that got added to the view
    #could have been done more cleanly by creating
    #a second view (one view being the display and
    #another being spoken messages)

  def RegisterView(self, view):
    self.views.append(view)
    #or just self.view = view

  def UpdateViews(self):
    for view in self.views:
      view.UpdateDisplay(self)

  #or UpdateView(self): if self.view: self.view.UpdateDisplay(self)

  def ProcessMove(self, move):
    """Model recieves "messages" from View here."""
    #modify internal state depending on the
    #information passed in "move",
    #for example move might say that the Piece in
    #a certain Cell was dragged to another Cell.
    #Here in ProcessMove you decide whether that's
    #a legal move, and if so what effect it should
    #have on the state of the game. Update state, then

    self.UpdateViews()

  #or there could be more than one "Process" method
  #if there are various different sorts of things the
  #players might do

class Square:
  def __init__(self, row, col):
    self.row = row
    self.col = col

#say a move is a drag from one square to another:

class Move:
  def __init__(self, start, end)
    self.start = start
    self.end = end

#wrote that out explicitly just to emphasize that
#the Move object, which the View passes to the
#Model as a message, doesn't know anything
#about the details of the display and also doesn't
#know anything about the rules of the game...

class View:

  def __init__(self, game):
    #set up the wxPython stuff
    self.game = game
    self.game.RegisterView(self)
    Bind(self, VariousEvents, VariousHandlers)

  def VariousHandlers(self, evt)
    #figure out what the players just did based
    #on evt. Maybe a player has just dragged a piece
    #from one square to another. figure out the row
    #and col of the start and end squares from 
    #information in evt.

    #Now tell the Model what just happened:

    self.game.ProcessMove(Move(Square(row1, col1),Square(row2, col2)))

  def UpdateDisplay(self):
    #look at self.game to figure out what pieces
    #go where, then draw them on the screen.
    wx.This
    wx.That

game = Model()
view = View(game)

>I think it's 
>mainly an issue of terminology, so probably I should just read up on MVC.
>
>> The user interface doesn't need to be graphical. There were games and 
>> emails clients and text editors before GUIs existed, you know ?
>
>Of course, but I'm specifically asking about creating a program that has a 
>GUI, and even more specifically it would be wxPython. 
>

David C. Ullrich



More information about the Python-list mailing list