On Dec 2, 2008, at 5:48 PM, Mikael Wämundson wrote:

In my project I'm using instances of NSMutableArray containing various objects. The arrays are used in the user interface through NSArrayControllers with binding. In IB I'm thus accessing the objects in the array through the controller.

In the code I have access to the array contents both through instances of the controllers and instances of the arrays. In my code, when do I use what? What is good programming? For example when sorting the array contents, adding objects, changing values for objects in the array…

Usually, the roles are arranged like this:

Model <-> Coordinating Controller <-> Mediating Controller (e.g. NSArrayController) <-> View


If you're writing code for the model layer, of course it should just directly operate on itself. If you're writing code for the coordinating controller layer, it should interact with the model directly, by going to its left in the diagram. It's pretty roundabout and nonsensical for it to interact with the array controller, because that will just have to turn around go back through the coordinating controller in order to access the model.

If the coordinating controller isn't trying to modify the model, but is trying to control the view, then it makes sense for it to sometimes directly operate on the mediating controller. For example, sometimes a sort operation is something you want to do to the model, to change the actual ordering of elements in a to-many relationship within the model. At other times, a sort operation is just for display. The model doesn't need to change; just the way it's shown. In the latter case, the coordinating controller would operate on the mediating controller and perhaps the view.

How do you tell the difference? Well, remember that separating the concerns is motivated by making the pieces reusable and independent. A given model could be represented by different views (a table, a pie chart, a web site, a command-line text-based listing, etc.). If the operation is something which should be reflected no matter which view or views are currently displaying the model, then it is an operation on the model. If the operation is something which is just specific to one particular view, then it should be handled by the mediating controller and the view.

Cheers,
Ken

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to