Model View Controller

Introduction

One of the most important core concepts for the beginning Cocoa programmer is the Model View Controller design pattern. MVC is central to good Cocoa application design as it is used extensively by Apple in Objective-C and the Cocoa frameworks.

What is MVC?

Mvc

It is useful to divide the complex task of computer application design into domains in order to simplify the process. Object oriented approaches are modular in philosophy, so the Model View Controller design pattern is a popular way to make logical divisions among class responsibilities.

Each object oriented programming environment/language has a slightly different definition/convention of MVC.

The main advantage of adopting a design pattern like MVC is that it allows the code in each unit to be decoupled from the others, making it more robust and immune to changes in other code.

Within the scope of Cocoa, MVC is an extremely important pattern. The major enhancements that Apple have made to Cocoa, namely Bindings and Core Data, are manifestly based on the MVC pattern.

Definitions

Model

A Model object:

  • is usually a simple subclass of NSObject (or an instance of NSManagedObject for CoreData)
  • has a set of instance variables in which to store its data
  • has a series of accessor methods for those ivars
  • has one or more init: methods to return new instances to other classes
  • has a dealloc method
  • may have custom methods to manipulate the model object’s internal data
  • is frequently reusable

View

A View object:

  • is some subclass of NSView
  • contains a drawRect: method which is the basis of all drawing
  • is rarely subclassed or modified
  • makes extensive use of delegates for customisation
  • is generally reusable

Controller

A Controller object:

  • mediates between model and view
  • is usually a subclass of NSObject
  • contains the outlets and actions for IB
  • contains ivars and collections to own and hold model objects
  • has methods for manipulating and composing model objects
  • contains the main awakeFromNib method
  • is instantiated in the nib file/s
  • contains the business logic of the program
  • is rarely reusable

History

Model View Controller was first proposed at Xerox PARC by Trygve Reenskaug of the SmallTalk design group, in order to simplify the design of object oriented gui apps.

Web References

Post new comment

The content of this field is kept private and will not be shown publicly.
Enter the code shown in the image:

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
9 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Donate!





If you like what you find here and wish to support further development of this site, please donate via PayPal. No account required.

Syndicate

Syndicate content

User login

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
15 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
Enter the code shown in the image: