Wednesday, September 12, 2012

Model-View-Controller (MVC) design pattern in iPhone


Model-View-Controller (MVC) :A design pattern in which the model (any data in your program), the view (what the user sees), and the controller (a layer that handles all interaction between the view and model) are separated in such a manner that modifying either the view or model component of your program has no effect on one another.



Model :The model contains the data.
View : The view displays information contained in the model.
Controller : the controller is responsible for accessing data from the model and displaying it on the view.

-------------------------- More Details with Example Got from SO--------------------------



Model : is related to a data which we have regarding our application's objects. Example : Application of a Person Contact Details than, Each Person on in Contact  will be having it'e own attributes (Name, Address etc.) and that will be saved inside Person model.
Controller : is something which controls UI updates. It keeps a reference to Person model and checks for any changes in model, If there is than change the View of that particular person. And it keeps checking for any UI input so it can change data inside Model.
View : This is fairly obvious View is all about UIView, What we see on screen.

Below is snap shot, how I follow MVC in my applications





Apple Says " The Model-View-Controller (MVC) design pattern assigns objects in an application one of three roles: model, view, or controller. The pattern defines not only the roles objects play in the application, it defines the way objects communicate with each other. Each of the three types of objects is separated from the others by abstract boundaries and communicates with objects of the other types across those boundaries. The collection of objects of a certain MVC type in an application is sometimes referred to as a layer—for example, model layer.

MVC is central to a good design for a Cocoa application. The benefits of adopting this pattern are numerous. Many objects in these applications tend to be more reusable "



Model Objects 
Model objects encapsulate the data specific to an application and define the logic and computation that manipulate and process that data.

View Objects
A view object is an object in an application that users can see. A view object knows how to draw itself and can respond to user actions. A major purpose of view objects is to display data from the application’s model objects and to enable the editing of that data. 

Controller Objects
A controller object acts as an intermediary between one or more of an application’s view objects and one or more of its model objects. Controller objects are thus a conduit through which view objects learn about changes in model objects and vice versa. Controller objects can also perform setup and coordinating tasks for an application and manage the life cycles of other objects.

No comments:

Post a Comment