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

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

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.

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

public class func createCompanyContact(
  companyName: String,
  phone: String
) -> ContactCard {
  
}
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
}
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")
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 2 Next: Introduction 3