Re: many-to-many relationships and object design
Re: many-to-many relationships and object design
- Subject: Re: many-to-many relationships and object design
- From: "Thomas L. Davis" <email@hidden>
- Date: Fri, 18 Jun 2004 08:59:05 -0500
On Jun 17, 2004, at 10:28 PM, mmalcolm crawford wrote:
On Jun 17, 2004, at 6:39 PM, Todd Ransom wrote:
My current model is simple - I have an NSDocument subclass. This
class has arrays of objects for actors and scenes. This works great
until I want to create a relationship between the two. An actor can
participate in multiple scenes and a scene can contain multiple
actors.
I have considered creating a relationship class that would act as a
table (like a relationship table in a RDBMS) but this seemed like it
would be complicated to implement. I tried storing an array of
actors in each scene object but then I needed a way for the scene to
resolve actor ids to actor objects. Since the actor objects only
know about themselves I put this in the NSDocument subclass. But now
I can't seem to think of a reasonable way for the scene objects to
know which instance of the NSDocument subclass they belong to. I get
the feeling that I am trying to work my way through a problem that
has a fairly simple solution if only I had more experience with OO
design.
Any pointers to info, suggested solutions, or links to source on this
would be greatly appreciated. I looked at Apple's docs on "object
modeling" but I couldn't turn the abstract information there into any
real ideas on how to implement this.
It's reasonable to model the many-to-many directly as arrays (although
you might instead use sets):
Actor <<->> Scene
@interface Actor : NSObject
{
NSMutableArray *scenes;
}
@interface Scene : NSObject
{
NSMutableArray *actors;
}
The main conceptual hurdle comes in manipulating the relationship. In
the RDBMS relationships are "implicit," so to find out what actors are
in a given scene you do a join. To manipulate relationships you
"simply" either add or remove records from the join table. In the
object world as suggested above relationships are "explicit,"
represented by arrays. Therefore to add an actor to a scene you
cannot simply add the actor to the scene's 'actors' array, to maintain
consistency of your object graph you must also ensure that the scene
is added to the actor's 'scenes' array. If you cut an actor from the
scene, you must ensure that both actor and scene are aware of the
deletion. It's not that difficult to implement, but takes a little
care and thinking about.
For more about object-relational mapping, you might care to browse
through
<http://developer.apple.com/documentation/WebObjects/
Enterprise_Objects/index.html>
Would it be possible and reasonable to maintain relationship
consistency by using bindings? This consistency problem seems very
common in OO designs (not just between the model and views). In my
limited experience, this is the most difficult part of designing and
writing OO code. I'm ready for a break-through in this department.
I think its interesting that RDBMS are mentioned. Perhaps a kind of
central database-like object should be used to maintain object
relationships. As it is, relationships tend to be scattered throughout
the code. Maybe its my procedural background, but I believe that
anything that would reduce the scatter and tend to centralize
functionality in OO code would be good.
Tom
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.