Model View Controller
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?
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.
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
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
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
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.