Modern software development is a combination of structured code, databases spanning a large number of interrelated tables and deadlines due yesterday. Today I'm going to discuss the Model-View-Controller architecture of organizing code.
History of MVCThe Model-View-Controlled architecture was created around 1970 - 1990 with several contributors adding to the concept over time. It was a 1988 paper (left) that transformed it into a general practice.
Originally when software was only command-line programs, the three sections were combined in the same place. As these applications moved beyond the command-line in favor of graphical interfaces, migrating the codebase became a challenging process and maintenance became a complex practice.
Getting StartedBefore I continue, I want to be clear, you can just as easily write your entire application in a single long page spanning thousands or more lines of code. This is perfectly acceptable if you don't mind the extended effort of maintaining the code in the future. However, modern software requires organized structure. Even the best commented code is hard to return to once you are not actively working on the original codebase. Instead of flooding a single page with code, your efforts tend to be better served when it's organized into multiple files, each file handling a specific segment of the application. Maintainability sky-rockets when you have structured code, especially if you are developing this with a team.
The BreakdownModel-view-controller separates the code into three distinct parts:
Model — the files that handles application logic, such as what data is saved, and how that data should react to certain commands, etc.
View — the output displayed to the user is part of the view. It takes the data from the Model and the Controller to display the information.
Controller — this section handles the user / system input, then informs the other two parts to make appropriate changes.
Example: Spreadsheet using MVCUsing a spreadsheet application as an example:
- Model would contain all the current data values from the spreadsheet
- View takes information from the Model to display some of the spreadsheet on the screen
- Controller manages the user's interaction with the application: mouse clicks and keyboard presses. It then translates them into commands for the Model and View components. It would also need to know something about the View, to identify what cell the user clicked on.
The example above is very simple. In a complete application, there are multiple models, views, and controllers. Each having a specific role, and when utilized as a whole, output the view.