Introduction 2

In the last demo, you created a contact card type using only Composition. The type is composed of the information of the card and also other contacts. However, this approach doesn’t make much sense.

Contact firstName lastName phoneNumber relatedContacts Contact Contact Contact
Composition Diagram

Your approach has two main issues:

  1. The contacts database in your app is the main array of contacts plus the contacts inside each contact’s relatedContacts.
  2. By deleting one contact, you’ll also completely delete all the related contacts from your database.

A simpler way to approach this is to use Aggregation and list all the contacts in an array. Contacts can have a link to other existing entries to create the relationship instead of containing the relative contact.

Contact Contact Contact firstName lastName phoneNumber relatedContacts firstName lastName phoneNumber relatedContacts firstName lastName phoneNumber relatedContacts
Aggregation Diagram

This means that each contact is composed of three string values and an aggregation of other contacts to form the relativeContacts.

You need to specify a way to link contacts to each other instead of having one contain another. This is similar to how you can open a file from different locations without duplicating the file itself using shortcuts (on Windows) or aliases (on macOS).

See forum comments
Download course materials from Github
Previous: Demo 1 Next: Demo 2