Wednesday, January 21, 2015

MVC ( Model View Controller ) in iOS - Part I

Hello Amigos!

   In this blog I have explained the MVC design pattern. Hope you'll like it.


   MVC (Model View Controller) is an architectural pattern for implementing user interface. The Model-View-Controller design pattern (MVC) and its variants have been around at least since the early days of Smalltalk. The design pattern is fundamental to many mechanisms and technologies in Cocoa Touch.


  It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user.The objects in MVC pattern can be segregated into one of the three roles in an application named "model, view or controller". MVC pattern also describes the way objects communicate with each other across the boundaries of their roles.  


Components of MVC and Roles Played By Them In Architecture






  • Model

Model represents the business logic of your application.It is the object that encapsulate the application data and defines how to manipulate it. These object are reusable as it represents the knowledge applicable to specific problem domain. Once any persistent data is loaded in application it must be put into a model object. In ideal case model object should not have any explicit association with the UI used for presenting and editing it.

A model notifies the controller when there is a change in state so that Controller can change its commands and send update command to the respective view. Model also takes command from the Controller to update it's state when there is a change in view on user action. 

As said Model are UI independent, and cant talk directly to controller.It always notifies Controller using broadcast mechanism.


  • View

These are the UI objects that represents what users see on device. A view object can respond to the user actions  and knows how to present itself on the screen. A view generally presents the data obtained from application. On any user user actions Controller commands the Model to update its state. 

Despite of the close relationship between the view object and model object, they both are not coupled with each other in an MVC application. View object never stores the data it is presenting. But it can cache it for some performance reasons. It has protocol to acquire data if needed

A view object can work with a portion of / complete / multiple model object. Thus they tend to be reusable and consistent across different applications.



  • Controller

Controller is the intermediary between one or more application view object  and one or more of its model object. It co-ordinates all work. controller can send commands to the model to update the model's state (e.g., editing a document). It can also send commands to its associated view to change the view's presentation of the model (e.g., by scrolling through a document). 

Apart from mediating role , controller object can other operations like managing life cycles of other object, performing setup and co-ordinating tasks for the application. Depending on the required design, controller objects can be either reusable or non- reusable.

View need to synchronise with controller. Controllers sets itself a s a view's delegate. Also it  drop a target on itself so that Model can communicate to it.

Figure below explains the Interaction between components:







MVC is not a bottom-line design pattern by itself. It contains several more relatively primitive design patterns.The elemental design patterns in MVC work together to define functional collaborations that are characteristic of an MVC application.The Cocoa (Touch) version of MVC comprises the Composite, Command, Mediator, Strategy, and Observer patterns. 


  • Composite : The view objects forms a view hierarchy in a coordinated manner. The view components in the hierarchy can range from compound views (such as table views) to individual views (such as text boxes or buttons). Each of the view nodes at any level can respond to user actions and draw itself onscreen.

  • Command : This is a target-action mechanism in which view objects can defer an execution on other objects, such as controllers, until certain events have occurred. The mechanism incorporates the Command pattern.

  • Mediator : A controller object plays a middleman role that adopts the Mediator pattern; it forms a bi-directional conduit for the flow of data between model and view objects. Changes in model are communicated to view objects through the controller objects of an application.

  • Strategy : A controller can be a “strategy” for any view object. A view object isolates itself in order to maintain its sole duty as a data presenter and delegates all application-specific decisions of the interface behaviour to its “strategy” object (the controller).

  • Observer : A model object keeps interested objects such as controllers updated with any changes to its internal state. 



In next post will give an example that implements MVC pattern. 

Reference : 
Objective-C Design Patterns for iOS - Carlo Chung
Wikipedia


No comments:

Post a Comment