Saturday, December 26, 2015

MVC (Model View Controller)

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.

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 objects 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