Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC the simplest example

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.

|- program: |—— controller.py |—— model.py |—— view.py |—— db.txt #simulates database 

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.

controller.py

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() 

view.py

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!'     

model.py

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 

So this is the scenario when the user wants to see all people in the db: enter image description here

Is this approach correct?

like image 593
Kesto Avatar asked Jun 26 '16 19:06

Kesto


People also ask

What is MVC pattern explain with example?

MVC Pattern stands for Model-View-Controller Pattern. This pattern is used to separate application's concerns. Model - Model represents an object or JAVA POJO carrying data. It can also have logic to update controller if its data changes. View - View represents the visualization of the data that model contains.

What is MVC and how it works?

-MVC is an architectural pattern consisting of three parts: Model, View, Controller. Model: Handles data logic. View: It displays the information from the model to the user. Controller: It controls the data flow into a model object and updates the view whenever data changes. -It is invented by Trygve Reenskau.

What is MVC in programming?

In object-oriented programming development, model-view-controller (MVC) is the name of a methodology or design pattern for successfully and efficiently relating the user interface to underlying data models.

Why is MVC used?

MVC is primarily used to separate an application into three main components: Model, View, and Controller. This level is considered the lowest level when compared with the View and Controller. It primarily represents the data to the user and defines the storage of all the application's data objects.


1 Answers

The MVC architecture is very broad and can change depending on the programming language and type of application you are doing, so in this case, yes your approach can be accepted as correct.

What I have learned from static typed languages is that you define the model and views as complete separate entities, and the controller takes an instance of both model and views as parameters.

What you need to ask yourself to define if your app is MVC is the following:

  • If I change something in the view do I break anything in the model?
  • If I change something in the model do I break anything in the view?
  • Is the controller communicating everything in both view and model so that they don't have to communicate with each other?

If nothing breaks and the controller does all of the communication then yes, your application is MVC.

You might want to look into design patterns such as Singleton, Factory and others that all use the MVC architecture and define ways to implement it.

like image 124
andrralv Avatar answered Sep 25 '22 14:09

andrralv