Model View Controller (MVC)
In the days before
object-oriented programming (and even for a time after object-oriented
programming became popular) there was a tendency to develop applications where the code for the user interface was tied
tightly to the code containing the application logic and data handling. This
coupling made application code difficult to maintain and locked the application
to a single user interface. If, for example, an application written for Microsoft
Windows needed to be migrated to Mac OS, all the code written specifically for
the Windows UI toolkits had to be ripped out from amongst the data and logic
code and replaced with the Mac OS equivalent. If the application then needed to
be turned into a web based solution, the process would have to be repeated
again. Attempts to achieve this feat were usually found to be prohibitively
expensive and ultimately ended up with the applications being completely
re-written each time a new platform needed to be targeted.
The goal of the MVC design pattern is to divorce the logic and
data handling code of an application from the presentation code. In this
concept, the Model encapsulates the data for the application, the View presents
and manages the user interface and the Controller provides the basic logic for
the application and acts as the go-between, providing instructions to the Model
based on user interactions with the View and updating the View to reflect
responses from the Model. The true value of this approach is that the Model
knows absolutely nothing about the presentation of the application. It just
knows how to store and handle data and perform certain tasks when called upon
by the Controller. Similarly, the View knows nothing about the data and logic
model of the application.
Within the context of an object-oriented programming environment
such as the iOS SDK and Objective-C, the Model, View and Controller components
are objects. It is also worth pointing out that applications are not restricted
to a single model, view and controller. In fact, an app can consist of multiple
view objects, controller objects and model objects.
The way that a view
controller object interacts with a Model is through the methods and properties
exposed by that model object. This, in fact, is no different from the way one
object interacts with another in any object-oriented programming environment.
In terms of the view controller’s interactions with the view, however, things
get a little more complicated. In practice, this is achieved using the
Target-Action pattern, together with Outlets and Actions.Model View Controller (MVC) |