Re: Parent-child Design Pattern
Re: Parent-child Design Pattern
- Subject: Re: Parent-child Design Pattern
- From: Joanna Carter <email@hidden>
- Date: Sun, 12 Jun 2011 20:22:30 +0100
Le 12 juin 2011 à 20:12, Martin Hewitson a écrit :
> This is a question about choosing the correct design pattern for a particular application. This application is generic enough that I can describe it by a simple parent-child model.
>
> Suppose I have a model object Parent which contains an array of Child objects.
>
> Then I have a ParentView object and a corresponding ParentViewController. The controller has a reference to the Parent and to the ParentView and so can act as a data source for the view. So far, so good.
>
> What I'm unsure about is, how to deal with the children objects. I will, in principle, have a ChildView. This raises some questions:
>
> 1) Should I have a ChildViewController class?
> 2) Where do I keep the array of ChildView classes? In the ParentView? And how do I ensure that each ChildView is associated with the appropriate Child object?
> 3) If I do have a ChildViewController class, where should I keep the array of these? In the ParentViewController?
>
> I can see a complicated spagehti system arising here. Suppose I don't only have a Parent-Child system, but a deeper tree (A contains an array of B, each B contains an array of C, etc), how should one extend the scheme?
>
> I'm guessing this is a generic enough situation that there exists a best-practice for handling it. For some reason, I can't arrive at it myself, so far. Any hints, keywords or clues would be well received.
This sounds like a typical tree structure. The basic structure is:
Node
Node *parent
NSArray *childNodes // contains instances of Node
NodeView
NodeView *parentView
NSArray *childViews // contains instances of NodeView
The Model takes this structure and so should the View.
I would say that you should design a NodeController and then derive from that class to add a ParentNodeController.
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