title: MVC the simplest example
body: <p>I'm struggling to understand the MVC pattern. I've been working with MVC frameworks like ASP.NET MVC and Django, but project structure there is pretty much forced, so it really didn't help to understand how to build my own apps based on this pattern. To clear things up i decided to write the simplest example of my understanding of MVC (console program in Python) and figure out if there is anything wrong.</p>
  
  <pre><code>|- program:
  |—— controller.py
  |—— model.py
  |—— view.py
  |—— db.txt #simulates database
  </code></pre>
  
  <p>So this is my basic structure. What this program will do is display all people that are inside db.txt. I use db.txt(json) to simulate actual database.</p>
  
  <p><strong>controller.py</strong></p>
  
  <pre><code>from model import Person
  import view
  
  def showAll():
      #gets list of all Person objects
      people_in_db = Person.getAll()
      #calls view
      return view.showAllView(people_in_db)
  
  def start():
      view.startView()
      input = raw_input()
      if input == 'y':
          return showAll()
      else:
          return view.endView()
  
  if __name__ == "__main__":
      #running controller function
      start()
  </code></pre>
  
  <p><strong>view.py</strong></p>
  
  <pre><code>from model import Person
  
  
  def showAllView(list):
      print 'In our db we have %i users. Here they are:' % len(list)
      for item in list:
          print item.name()
  def startView():
      print 'MVC - the simplest example'
      print 'Do you want to see everyone in my db?[y/n]'
  
  def endView():
      print 'Goodbye!'    
  </code></pre>
  
  <p><strong>model.py</strong></p>
  
  <pre><code>import json
  
  class Person(object):
  
      def __init__(self, first_name = None, last_name = None):
          self.first_name = first_name
          self.last_name = last_name
      #returns Person name, ex: John Doe
      def name(self):
          return ("%s %s" % (self.first_name,self.last_name))
  
      @classmethod
      #returns all people inside db.txt as list of Person objects
      def getAll(self):
          database = open('db.txt', 'r')
          result = []
          json_list = json.loads(database.read())
          for item in json_list:
              item = json.loads(item)
              person = Person(item['first_name'], item['last_name'])
              result.append(person)
          return result
  </code></pre>
  
  <p>So this is the scenario when the user wants to see all people in the db:
  <a href="https://i.stack.imgur.com/Fkooj.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Fkooj.png" alt="enter image description here"></a></p>
  
  <p>Is this approach correct?</p>