Object-Oriented Programming: Beyond the Basics

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

Lesson 04: Single Responsibility & Open-Closed Principles

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.

Open the starter playground. It’s identical to the code you had at the end of lesson three, “Design Patterns”.

public struct ContactOptions: OptionSet {
  public init(rawValue: Int) {
    self.rawValue = rawValue
  }
  public let rawValue: Int
}
static let unknown = ContactOptions(rawValue: 1 << 0) // binary = 0001
static let person = ContactOptions(rawValue: 1 << 1) // binary = 0010
static let company = ContactOptions(rawValue: 1 << 2) // binary = 0100
static let emergency = ContactOptions(rawValue: 1 << 3)// binary = 1000
static let publicPayPhone = ContactOptions(rawValue: 1 << 4) // binary = 10000
var canAdd: ContactOptions = []
var cardType: ContactOptions = .unknown
public final func addRelatedContact(_ contact: ContactCard) {
public final func addRelatedContact(_ contact: ContactCard) {
  if canAdd.contains(contact.cardType) {
    print("Adding contact to related list.")
    relatedContacts.append(contact.contactID)
  } else {
    print("Can't Add")
  }
  if contact.canAdd.contains(cardType) {
    print("Adding inverse relation")
    contact.relatedContacts.append(self.contactID)
  } else {
    print("Can't Add Inverse")
  }
  broadcastUpdates()
}
public override init(firstName: String, lastName: String, phoneNumber: String) {
  super.init(firstName: firstName, lastName: lastName, phoneNumber: phoneNumber)
  canAdd = [.person] // new code
  cardType = .person // new code
}
override init(firstName: String, lastName: String, phoneNumber: String) {
  super.init(firstName: firstName, lastName: lastName, phoneNumber: phoneNumber)
  canAdd = [.person, .company] // new code
  cardType = .company // new code
}
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 3 Next: Conclusion