Re: MVC question about updating the Model
Re: MVC question about updating the Model
- Subject: Re: MVC question about updating the Model
- From: Joanna Carter <email@hidden>
- Date: Fri, 16 Apr 2010 14:56:50 +0100
Hi Matt
> I have multiple resource objects that are represented in an
> NSOutlineView. These resource objects can be updated by their own
> triggered timer or potentially on demand by the user via the View (a
> button is clicked in the UI).
>
> At first, I thought that the resource object should have the
> capability to update itself. Therefore, the resource has a method like
> so:
>
> [resource update];
>
> If the resource's timer fires, it calls it's update method like so:
>
> [self update];
>
> If the resource update button is clicked by the user, the selected
> resource item is updated by the NSWindowController with:
>
> [resource update];
>
> This is where I'm having difficulty determining what should be
> standard practice. Would it be better to post a notification that
> indicates the resource should update itself? Or is it good enough to
> leave it as is?
>
> One other approach I've considered is whether the Model should have
> the ability to update itself at all. Should the controller be
> responsible for updating the resource always?
>
> It seems that either approach is viable, but which is better?
When designing an OO system, the guiding principle should always be "Responsibility Driven Design".
In other words, what determines when the resource should be updated? If the resource can be responsible for keeping itself updated, then that is where the update method belongs and it can be called from an internal timer method, if that is the only way to "detect" the new state.
If the resource doesn't know how to update itself, then the update method can be called from any other code, including a controller.
My guess is that you have a resource that know how to update its state and that change in state is observed by the controller/UI. However, you also want to be able to provide a user with an update button, in case they want to ensure that the state is up to date between automatic refreshes. In which case, the controller handles the action of the button and calls the update method on the resource.
Joanna
--
Joanna Carter
Carter Consulting
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden