Object-Oriented Programming: Beyond the Basics

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

Lesson 03: Design Patterns

Demo 2

Episode complete

Play next episode

Next
Transcript

In the last demo, you implemented the Singleton pattern on ContactsBook. In this demo, you’ll implement the Factory pattern. Select the Sources folder. Click File ▸ New File, then name your new file ContactsFactory.

Next, add the following:

public class ContactsFactory {
}

The factory is responsible for creating the two contact types, so you need to create methods to build each one:

public class func createPersonContact(
  firstName: String,
  lastName: String,
  phone: String
) -> ContactCard {
  
}

public class func createCompanyContact(
  companyName: String,
  phone: String
) -> ContactCard {
  
}

Notice the new keyword in the method declaration: class. Just as you can access static properties directly from the class without creating an object, you can similarly access methods in the same way. Defining a method as a class lets you call it directly from ContactsFactory.createPersonContact(:::).

Now, add the implementation in each function by calling the constructors of the child types:

public class func createPersonContact(
  firstName: String,
  lastName: String,
  phoneNumber: String
) -> ContactCard {
  PersonContactCard(firstName: firstName, lastName: lastName, phoneNumber: phone) // new code
}

public class func createCompanyContact(
  companyName: String,
  phoneNumber: String
) -> ContactCard {
  CompanyContactCard(companyName: companyName, phoneNumber: phone) // new code
}

Here, you’re creating objects from PersonContactCard and CompanyContactCard, but you’re returning them as ContactCard.

Finally, go to the main playground file and replace the code where you create the different instances of the contact cards to use the new Factory methods:

let ehabContact = ContactsFactory.createPersonContact(firstName: "Ehab", lastName: "Amer", phoneNumber: "1234567890")
let timContact = ContactsFactory.createPersonContact(firstName: "Tim", lastName: "Condon", phoneNumber: "0987654321")

...

let kodeco = ContactsFactory.createCompanyContact(companyName: "Kodeco", phoneNumber: "1111111111")
let razeware = ContactsFactory.createCompanyContact(companyName: "Razeware", phoneNumber: "2222222222")

When you call those methods from your playground, notice that Xcode shows their signature when they return an instance of ContactCard.

Run the program. Nothing else in your system needs to change.

With this example, Factory patterns might seem like more trouble than they’re worth. However, you’ll meet situations where creating an object isn’t as simple as calling a single constructor. You may have many configurations to apply to the object, and you might need to perform multiple methods and steps before the object is ready to use. The Factory pattern is ideal for those situations.

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