Popular Tags

Who's online

There are currently 0 users and 71 guests online.

User login

In Cocoa programming, the Model-View-Controller design paradigm (MVC for short) is the most common design pattern for construction programs.

The basic idea behind MVC is that a program can be divided logically into three different sections:

  • The Model - this represent the data structure of the program. Most likely it will include data storage classes that contain logic for manipulating data internally. In more recent versions of the Cocoa frameworks, Core Data has been included as a way for programmers to build complex data models with relative ease.
  • The View - The view is basically what the user sees (the interface). Most View-related things will be handled by the AppKit framework.
  • The Controller - This is where the heart of your program is. The Controller is what takes the data from the Model and connects it with the View. It is the "glue code" of your application.

Example

In BYU's Computer Science program, students are required to take CS 240 - Advanced Programming Concepts, which includes a project on writing a Chess game (without AI). The interface is provided to the students (as some GTK classes), and students are in charge of creating the Model and the Controller. Here's an example of how this might be accomplished using MVC:

  • The view is provided as a set of classes, and there is one class in particular, the "chess" class, which is the gateway to the rest of the interface. For all intents and purposes, it is the only class with which students need to interact.
  • The model is heart of the program. This author set his up in the following way:
    • Player class - this class represents a player (Black or White) and manipulates the pieces for that player.
    • Piece protocol - this protocol (similar to Java's Interfaces or C++'s virtual classes) defines the basic properties and methods for all pieces, such as moveToSquare:, type:, currentPosition:, canAttackTo:, canMoveTo:, and so on.
    • various piece classes - a set of classes (King, Queen, Rook, etc) that implement the Piece protocol and contain the logic specific to the behavior of the individual piece.
  • The controller is where it all comes together. The controller contains two instances of Player, one for Black and one for White. The controller tracks whose turn it is, if the current player is in check, if the game is a stalemate, and so on. This is done by accessing the various methods of the Player class and asking for information. For example, the controller may ask for a set of all possible moves of all remaining pieces, or it may ask for the position of the King, etc. Once the controller has received all of the information it needs, it relays that information on to the View, where it is displayed to the user.

One of the obvious advantages of MVC is that it allows for code recycling. A model that is not directly tied to an interface is a model that can be reused in other programs. This can save quite a bit of work for the programmer. In addition, MVC provides effective data privacy. Objects can only access data they specifically need, preventing classes from exposing potentially "dangerous" portions of code directly to the user.

References

Your rating: None Average: 1 (1 vote)

Search

UPDATES

The site has recently been updated. You may notice some of the following issues:

  • Some URLs no longer work. Please use the search box.
  • File uploads should now be working. If you experience problems, please contact us.