MVC the simplest example
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.
Kesto
Currently studying software engineering. Mostly interested in web app development.
Updated on June 09, 2021Comments
-
Kesto almost 3 years
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?
-
user32882 about 2 yearsSingleton and Factory are not implemented in "traditional" MVC. The GOF book only mentions Strategy, Composite and Observer. Observer models the Model/View relationship. The View itself may be a Composite and the Controller often implements a Strategy to communicate with the View.