By reference I mean an instance variable. So it's the same thing.

But it's not that one object has two "parents" (this isn't a tree like
structure).
The object (it is, the Track) has two references to the Episodes where
it appears, such reference happens because aTrack as a instance
variable "holding" (aka "referencing") aCollection, and this
collection "contains" (it is, references) two elements, that are the
Episodes in which a track appears.

For the sake of simplicity, you should define who must perform the
"maintenance" of the collections on only one sides. In my opinion it
should be the Episode.

E.g.
Episode>>addTrackToPlaylist: aTrack
  self playlist add: aTrack.
  aTrack includeInEpisode: self

Episode>>playlist
  playlist ifNil: [playlist := OrderedCollection new].


Track>>includeInEpisode: anEpisode
   self episodes add: anEpisode

Track>>#episodes
   episodes ifNil: [episodes := Set new]


You can see that #includeInEpisode: doesn't add itself to the Episode,
because it is very likely that the interaction is started onto the
Episode and not the Track. Otherwise to avoid a recursion you should
have a condition like:

Episode>>addTrackToPlaylist: aTrack
  (self playlist includes: aTrack) ifFalse: [
    self playlist add: aTrack.
    aTrack includeInEpisode: self ]

Track>>includeInEpisode: anEpisode
   self episodes add: anEpisode.
   anEpisode addTrackToPlaylist: self




The paper about "Mutual Friends" talks about the pro's and cons of
different approaches.

Esteban A. Maringolo

On Mon, Jun 10, 2019 at 2:40 PM sergio ruiz <sergio....@gmail.com> wrote:
>
> when you say references, how does this look in pharo? I don’t know that I 
> have used such a relationship.
>
> I was thinking that an object would have an instance variable that was an 
> ordered collection of objects.. So having one object belong to two parent 
> objects could be tricky.
>
>
>
> On Jun 10, 2019, at 1:27 PM, Esteban Maringolo <emaring...@gmail.com> wrote:
>
> You have anEpisode that references a collection with instances of Track.
> And each Track has a collection of episodes.
>
>
> ----
> peace,
> sergio
> photographer, journalist, visionary
>
> Public Key: http://bit.ly/29z9fG0
> #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
> http://www.codeandmusic.com
> http://www.twitter.com/sergio_101
> http://www.facebook.com/sergio101
>

Reply via email to