- Exception Handling
- Python Design Patterns - Anti
- Concurrency in Python
- Strings & Serialization
- Python Design Patterns - Queues
- Python Design Patterns - Sets
- Lists Data Structure
- Dictionaries
- Python Design Patterns - Iterator
- Object Oriented Concepts Implementation
- Object Oriented
- Abstract Factory
- Python Design Patterns - Flyweight
- Python Design Patterns - Template
- Python Design Patterns - Strategy
- Python Design Patterns - State
- Python Design Patterns - Observer
- Chain of Responsibility Pattern
- Python Design Patterns - Proxy
- Python Design Patterns - Decorator
- Python Design Patterns - Adapter
- Python Design Patterns - Command
- Python Design Patterns - Facade
- Python Design Patterns - Prototype
- Python Design Patterns - Builder
- Python Design Patterns - Factory
- Python Design Patterns - Singleton
- Model View Controller Pattern
- Python Design Patterns - Gist
- Introduction
- Python Design Patterns - Home
Python Design Patterns Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Model View Controller Pattern
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 −
Let us now see how the structure works.
Model
It consists of pure apppcation 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 pstens to the events triggered by view and queries model for the same.
Python code
Let us consider a basic object called “Person” and create an MVC design pattern.
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 pst of Person objects def getAll(self): database = open( db.txt , r ) result = [] json_pst = json.loads(database.read()) for item in json_pst: item = json.loads(item) person = Person(item[ first_name ], item[ last_name ]) result.append(person) return result
It calls for a method, which fetches all the records of the Person table in database. The records are presented in JSON format.
View
It displays all the records fetched within the model. View never interacts with model; controller does this work (communicating with model and view).
from model import Person def showAllView(pst): print In our db we have %i users. Here they are: % len(pst) for item in pst: 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!
Controller
Controller interacts with model through the getAll() method which fetches all the records displayed to the end user.
from model import Person import view def showAll(): #gets pst 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()Advertisements