Example implementation of associations with Slots in Pharo 4.0

With it you can link two slots together so an update on one side will also update the other side of the association/relationship.

I used these Movie and Person classes for testing:

Object subclass: #SlotExampleMovie
slots: {
#name.
#year.
ToOneAssociationSlot named: #director target: #SlotExamplePerson inverse: #directedMovies.
ToManyAssociationSlot named: #actors target: #SlotExamplePerson inverse: #actedInMovies }
classVariables: { }
category: ‘SlotAssociations-Tests-Example’

Object subclass: #SlotExamplePerson
slots: {
#name.
ToManyAssociationSlot named: #directedMovies target: #SlotExampleMovie inverse: #director.
ToManyAssociationSlot named: #actedInMovies target: #SlotExampleMovie inverse: #actors }
classVariables: { }
category: ‘SlotAssociations-Tests-Example’

With this definition the director of a Movie must be a Persion instance. When you set the director of a movie this movie is automatically added to the directedMovies collection of that person. Off course it also works the other way around.

It took very little code to add these association slots. I think Slots are a really nice addition to Pharo! If anyone wants to have a look at the code:

MCHttpRepository
location: ‘http://smalltalkhub.com/mc/JanVanDeSandt/Stuff/main’
user: ”
password: ”

Jan.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: