Object-Oriented Programming: Beyond the Basics

Oct 17 2023 · Swift 5.9, iOS 17, Xcode 15

Lesson 01: Composition & Aggregation

Demo 1

Episode complete

Play next episode

Next
Transcript

You’ll start by building the base for your contacts program. In this demo, I’m using an Xcode playground and writing in Swift. If you’re following along, start up Xcode and select File ▸ New ▸ File. Choose Playground. Name it Composition and save it to your desktop.

The first thing you need to understand is the app’s purpose and requirements.

Each contact carries basic information:

  • First name
  • Last name
  • Phone number

So first, you’ll define the Contact entity as follows:

struct ContactCard {
  var firstName: String
  var lastName: String
  var phoneNumber: String
}

Here, you defined a structure named ContactCard that contains three properties of type String. You defined the phone number as a string to avoid any issues related to having country codes or the + (plus) character, which you’ll need in some phone numbers.

Next, create two contacts with the new type. Add the following code after the declaration of the structure in your playground file:

var ehabContact = ContactCard(firstName: "Ehab", lastName: "Amer", phoneNumber: "1234567890")
var timContact = ContactCard(firstName: "Tim", lastName: "Condon", phoneNumber: "0987654321")

Those two lines define different contacts with different information using the model you created.

So far, you’ve been using Composition to define your object. ContactCard is composed of a string for the first name, a string for the last name and a string for the phone number.

The object owns the data directly, and deleting any contact will delete all the information contained inside it.

The next requirement is to connect contacts to one another. Some contact book apps allow you to connect different contacts together with a relationship. For example, you can save the contacts of a whole family and specify the relationship status as brother, sister, father, mother, etc.

Your app should be able to connect contacts to one another. However, for simplicity, you won’t define the nature of the relationship.

One way to think about this is by adding a new property for a list of related contacts inside the contact card:

struct ContactCard {
  var firstName: String
  var lastName: String
  var phoneNumber: String
  var relatedContacts: [ContactCard] = []
}

Note that you give the array a default value of an empty array so you don’t need to make any changes to the existing code defining ehabContact and timContact.

Then, add the following lines after the definition of the two contacts to create a relationship between them:

ehabContact.relatedContacts.append(timContact)

Now, you’ll print out the contact.

print(ehabContact.relatedContacts)

Run the playground. You can see the Tim contact prints out to the console. So this works, but… it doesn’t make sense?

See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 1 Next: Introduction 2