Monday, April 20, 2015

Model - View - Controller (MVC)



iOS programs are functionally divided into three parts: model, view and controller. The model takes care of business logic. In a game, it would be keeping score, remembering moves and detecting invalid moves. In a text editor, it would store the file content (before saving it to disk), remember user changes if implementing an undo function, and carry out search and replace tasks.

The view implements the user interface. It provides the buttons on which the user clicks and any other controls with which the user interacts with the application.

The controller is responsible for dispatching messages between the view and the controller. It makes user interface objects appear and disappear, or change their appearance, and informs the model when the user has performed a UI action such as clicking on a button. Conversely, it informs the view when changes in the data managed by the model mean that the UI must be updated.

MVC architecture dates back to the mid-1970s when it was used in Smalltalk, an early object-oriented programming environment. Microsoft also uses MVC architecture in Windows programming, but has moved away from MVC terminology in recent technical papers. In fact, MVC architecture lends itself well to any message-driven programming environment. By “message-driven”, I mean an environment where a central dispatcher (controller) manages system messages that are triggered by user interface (view) actions such as mouse clicks, and where various snippets of business logic code (in the model) are executed in response.

Functionally separating code in this way makes code easier to read because user interface code and business logic are kept separate. In the 1980s and early 1990s it facilitated client-server architectures where the business logic ran on a different machine than the user interface. This is also the architecture of the web, where thin, browser-based clients implement the view, powerful web servers handle the model (often coordinating with other server-side resources such as database servers), and HTTP, the protocol of the web itself, serves as the controller.

No comments:

Post a Comment