The main aim of the MVC architecture is to separate the business logic and application data from the presentation data to the user.
Here are the reasons why we should use the MVC design pattern.
- They are resuable : When the problems occurs, there is no need to invent a new solution, we just have to follow the pattern and adopt it as necessary.
- They are expressive: By using the MVC design pattern our application becomes more expressive.
The Model-View-Controller (MVC) design pattern is a way of dividing your code into independent functional areas. The model portion defines your application’s underlying data engine and is responsible for maintaining the integrity of that data. The view portion defines the user interface for your application and has no explicit knowledge of the origin of data displayed in that interface. The controller portion acts as a bridge between the model and view and facilitates updates between them.
1). Model: The model object knows about all the data that need to be displayed. It is model who is aware about all the operations that can be applied to transform that object. It only represents the data of an application. The model represents enterprise data and the business rules that govern access to and updates of this data. Model is not aware about the presentation data and how that data will be displayed to the browser.
2). View : The view represents the presentation of the application. The view object refers to the model. It uses the query methods of the model to obtain the contents and renders it. The view is not dependent on the application logic. It remains same if there is any modification in the business logic. In other words, we can say that it is the responsibility of the of the view's to maintain the consistency in its presentation when the model changes.
3). Controller: Whenever the user sends a request for something then it always go through the controller. The controller is responsible for intercepting the requests from view and passes it to the model for the appropriate action. After the action has been taken on the data, the controller is responsible for directing the appropriate view to the user. In GUIs, the views and the controllers often work very closely together.
MVC in iPhone:
The Model, View, Controller (MVC) design pattern is one way of looking at software development that the Apple engineers put into the Cocoa frameworks (part of the iPhone system). Here are the three pieces of the MVC pattern:
- Model: the abstract data about your problem space
- View: the visual elements of the application
- Controller: code that glues the model and view together
As an example, think of an iPhone app that is used to maintain a task list. The app would simply display a list of tasks that users check off as they are completed. Here is how the MVC pattern would fit into this app.
Our example app model would be an object that contains the list of tasks, the state of each task and some code to make the task data persistent.
Our view would be an object that contained a table with cells that allowed text entry and user interaction (GUI elements).
Our app’s controller object would know about the Model and the View. Controller will respond to user input and inform the view to update it’s visual elements or have the Model perform some action to the underlying data.