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:
Is this approach correct?
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.
-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.
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.
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.
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With