You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Model View Controller is the most commonly used design pattern. Developers find it easy to implement this design pattern.
Following is a basic architecture of the Model View Controller
Model
It consists of pure application logic, which interacts with the database. It includes all the information to represent data to the end user.
View
View represents the HTML files, which interact with the end user. It represents the model’s data to user.
Controller
It acts as an intermediary between view and model. It listens to the events triggered by view and queries model for the same.
# model.pyimportjsonclassPerson:
def__init__(self, first_name=None, last_name=None):
self.first_name=first_nameself.last_name=last_name#returns Person name, ex: John Doedefname(self):
return ("%s %s"% (self.first_name,self.last_name))
@classmethod#returns all people inside db.txt as list of Person objectsdefgetAll(cls):
database=open('db.txt', 'r')
result= []
json_list=json.loads(database.read())
foriteminjson_list:
item=json.loads(item)
person=Person(item['first_name'], item['last_name'])
result.append(person)
returnresult
# view.pyfrommodelimportPersondefshowAllView(list):
print'In our db we have %i users. Here they are:'%len(list)
foriteminlist:
printitem.name()
defstartView():
print'MVC - the simplest example'print'Do you want to see everyone in my db?[y/n]'defendView():
print'Goodbye!'
# controller.pyfrommodelimportPersonimportviewdefshowAll():
#gets list of all Person objectspeople_in_db=Person.getAll()
#calls viewreturnview.showAllView(people_in_db)
defstart():
view.startView()
input=raw_input()
ifinput=='y':
returnshowAll()
else:
returnview.endView()
if__name__=="__main__":
#running controller functionstart()