Day 12

Model-View-Controller

Model-View-Controller is what is known as a software design pattern.

In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations. Patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.

-- Wikipedia article on Software Design Pattern

The Model-View-Controller design pattern (or MVC for short) is an extremely useful design pattern for a number of applications. The most common places that it shows up are graphical user interfaces and web applications. Most importantly it is ideally suited to the projects that you all will be doing for this mini-project. Here is a figure that shows the basic principles of the MVC design pattern.

  • A controller can send commands to the model to update the model’s state (e.g. editing a document). It can also send commands to its associated view to change the view’s presentation of the model (e.g. by scrolling through a document).

  • A model stores data that is retrieved according to commands from the controller and displayed in the view.

  • A view generates an output presentation to the user based on changes in the model.

-- Wikipedia article on Software Design Pattern

This decomposition has a number of extremely nice properties. At the highest level, the pattern allows for the writing of loosely coupled and highly modular code. This allows various components to be swapped out with minimal changes to the overall program. However, in order to see the full power of MVC, it helps to go through one cycle of using it to solve a problem.

Brick Breaker

We have spent some time getting you familiar with objects in Python. We now need to explore why they are powerful. We will build up an example project together that is in the video game space – so that you can be in a position to transfer your learning to your homework assignment.

Let’s see how far we can get into making a game similar to a classic – Breakout for the Atari 2600.

We will utilize the Model-View-Controller design method, to give everyone a concrete example of the kind of architecture we expect you to produce in your homework.

The code will rely upon pygame so make sure that it is installed – see the instructions for Mini-Project 4 to get Pygame set up if you have not done so.

brickbreaker-sp17.py

Bouncy Ball

I will start with a file bounce.py, and refactor it to use MVC.

It will end going through something like the following progression:

Optional exercise

Refactor spin.py to use MVC. Optional optional: add some flair to it.