MVC Design Pattern in Cocoa
2017-04-07 10:25
369 查看
MVC Design Pattern in Cocoa
Posted on May6, 2014 by Debasis
Das — No
Comments ↓
MVC Design Pattern in Cocoa
High level pattern that classifies objects based on the roles they play in an applicationIts a compound design pattern and comprises of several elemental design pattern
e.g
of mvc pattern in Cocoa can be found in Cocoa Bindings, Cocoa Document Architecture, Scriptability etc
MVC Pattern is divided into objects
Model ObjectsView Objects
Controller Objects
If developers are adhering to this Design pattern and are creating their custom classes, the custom classes must fall into one of the 3 categories (model, view or controller)
Model
Holds data and defines logic that manipulates that dataAll persistent data must reside in models.
Model objects should not be concerned with interfaces and presentation issues.
Views
Knows how to display the data and allows the users to edit itViews should not be responsible for storing the data that it is displaying
Caching Data in View Objects is an exception for performance reasons.
Views can be responsible for rendering a part of the model objects or the whole model object
A large number of view objects can be found in AppKit Framework
Controllers
Acts as an intermediary between the applications view and model objectsActs as a conduit through which the view objects knows about the change in the model objects
Controllers are also responsible for managing the lifecycle of other objects
Combining Roles in MVC Design Pattern
View Controller
Fulfills the role of both the controller and the viewIt owns the interface (views).
Main responsibility is to manage the interface and communicate with the models.
Model Controller
Owns the model.Main responsibility is to manage the model and communicate its changes to view objects
Types of Controller Objects
Mediating Controller
Inherits from NSController classUsed in Cocoa Bindings
Some ready to use Mediating Controller classes are
NSObjectController
NSTreeController
NSArrayController
NSUserDefaultsController
Facilitates, Mediates the flow of data between view and model objects
Coordinating Controller
Provides services such as responding to delegation messagesResponding to action messages
Managing the lifecycle of objects (Windows, View Objects, Instance variables, Mediating Controllers etc)
Establishing connection between objects
Example of Co-ordinating controllers are
NSWindowController
NSDocumentController
The pattern as a (Composite, Strategy and Observer ) design
pattern
Composite
View Objects are a composite of nested views (view hierarchy)
Strategy
View objects confines itself to maintaining its visual aspect and delegates all the decision about the application specific meaning to the controller
Observer
Model objects keeps the view objects apprised/advised about the changes in its state.![](http://www.knowstack.com/wp-content/uploads/2014/05/MVC_Traditional.png)
Traditional MVC Design Pattern
![](http://www.knowstack.com/wp-content/uploads/2014/05/MVC_Cocoa1.png)
Cocoa Version of MVC Design Pattern
![](http://www.knowstack.com/wp-content/uploads/2014/05/MVC_Cocoa2.png)
MVC Coordinating Controller
Design Guidelines
For mediating controller, use an instance of NSObjectController, NSArrayController, NSTreeController rather than implementing all the functionalities in a NSObject subclassKeep the roles separated for maximum reusability
In case you are planning to merge roles, then pick the predominant role for a class and use categories to extend the class to play other roles
Although it is possible to have views directly observe the properties of model objects, it is not a best practice. Avoid the temptation. A view should always go through a mediating controller to learn about the changes in a model object
A view class should not depend on a model class (If this is unavoidable, reduce the dependency)
A model class should not depend on anything other than model classes
A mediating controller should not depend on model classes, view classes or co-ordinating controllers.
![](http://www.knowstack.com/wp-content/uploads/2014/05/MVC_Cocoa3-1024x721.png)
Sample Implementation
相关文章推荐
- MVC design pattern in our project, the Order system
- Cocoa.DesignPattern.Factory
- Thinking In Design Pattern——MVP模式演绎
- Design Patterns in Smalltalk MVC 在Smalltalk的MVC设计模式
- MVC Design Pattern
- Abstract Classes and Factory Design Pattern in Python
- Thinking In Design Pattern——MVP模式演绎
- Prototype design pattern in Java
- CRUD Operations Using the Generic Repository Pattern and Dependency Injection in MVC
- Thinking In Design Pattern——Query Object模式
- Template Method Design Pattern in Java
- It's bad design that leveraging singleton design pattern in db connection
- 状态模式在领域驱动设计中的使用(Using the State pattern in a Domain Driven Design)
- Thinking In DesignPattern
- Thinking In DesignPattern
- Builder design pattern in java
- Design Pattern in Simple Examples
- Thinking In Design Pattern——探索SOA在企业应用程序中扮演的角色
- CRUD Operations Using the Generic Repository Pattern and Dependency Injection in MVC
- Thinking In Design Pattern——工厂模式演绎