Model View Controller (MVC)

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 2 minutes

Model View Controller in Python

Model View Controller in Python

Design Patterns Home
 

What is it?

Model-View-Controller is the most popular Architectural Pattern practised in the industry. It is widely used to design web applications and desktop GUIs i.e. Graphical User Interfaces. It is based on the idea of dividing the application into three parts i.e. Model, View and Controller.

  • The Model houses the attributes of the primary entity. For example, assume that you are making a web application which renders famous quotations. These famous quotations have two attributes: the quotation itself, and its speaker. The Model will be a class with these two attributes.
  • The View is responsible for rendering the information on the front-end. The front-end is what the user sees, and could be realised using a webpage (in case of web applications) or a GUI toolkit such as Tkinter (in case of Desktop Applications).
  • The Controller creates objects of the model, is capable of manipulating its attributes, and sends commands to the associated view to render the information on the front-end.

Django, a famous Web Framework is based on the MVC architecture.


How to implement it

class FamousQuotation(object):
'''Model: houses two attributes of every Famous Quotation: speaker and the quote.'''
def __init__(self, speaker, famousQuotation):
self.speaker = speaker
self.famousQuotation = famousQuotation

class FamousQuotationGUI(object):
'''View: responsible for rendering information to the front-end of the application. For this demonstration, let's just print the information on the console.'''
def printQuotationDetails(self, speaker, famousQuotation):
print('"{}" ~ {}'.format(famousQuotation, speaker))

class FamousQuotationController(object):
'''Controller: creates objects of the model, is capable of manipulating its attributes, and sends commands to the
associated view to render the information on the front-end.'''
def __init__(self, model, view):
self.model = model
self.view = view
def setFamousQuotationSpeaker(self, speaker):
self.model.speaker = speaker
def getFamousQuotationSpeaker(self):
return self.model.speaker
def setFamousQuotationQuotation(self, famousQuotation):
self.model.famousQuotation = famousQuotation
def getFamousQuotationQuotation(self):
return self.model.famousQuotation
def renderInformationOnFrontEnd(self):
self.view.printQuotationDetails(self.model.speaker, self.model.famousQuotation)

famousQuotationOne = FamousQuotation('M K Gandhi', 'An eye for an eye ends up making up the whole world blind.')
famousQuotationGUI = FamousQuotationGUI()
controllerOne = FamousQuotationController(famousQuotationOne, famousQuotationGUI)

controllerOne.renderInformationOnFrontEnd()
controllerOne.setFamousQuotationSpeaker('Mohandas K Gandhi')
controllerOne.renderInformationOnFrontEnd()

### OUTPUT ###
"An eye for an eye ends up making up the whole world blind." ~ M K Gandhi
"An eye for an eye ends up making up the whole world blind." ~ Mohandas K Gandhi

How an Architectural Pattern differs from a Design Pattern

An Architectural Pattern emphasizes on utilizing resources effectively. Different parts of an Architectural Pattern can be materialized using different technologies/programming languages. For example, in Django, Models and Controllers are written using Python, while Views are written in HTML and Javascript.

A Design Pattern offers a methodical way of writing effective code which is readable, reusable and maintainable. It provides a reusable solution to a design problem. All parts of a Design Pattern are materialized using the same technology (programming language). This is because writing different classes in different languages makes little sense.


 

 

 

 


See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply