Object-Oriented Programming: Beyond the Basics

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

Lesson 05: Liskov Substitution, Interface Segregation & Dependency Inversion

Demo 1

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this demo, you’ll put the Liskov Substitution to work. You’ll pick up right where you left off with the contacts app in the previous lesson.

public class EmergencyContactCard: ContactCard {
  override private init(firstName: String, lastName: String, phoneNumber: String) {
    super.init(firstName: firstName, lastName: lastName, phoneNumber: phoneNumber)
    canAdd = []
    cardType = .emergency
  }
 public convenience init?(emergencyName: String, phoneNumber: String) {
  if phoneNumber.count != 3 {
    return nil
  }
    self.init(firstName: emergencyName, lastName: "", phoneNumber: phoneNumber)
  }
 public override func contactInformation() -> String {
    "Contact: EmergencyName: \(firstName), Phone: \(phoneNumber)"
  }
}
public override func set(phone: String) {
  guard phone.count == 3 else {
    return
  }
  super.set(phone: phone)
}
public func set(firstName: String, lastName: String) -> Bool {
  self.firstName = firstName
  self.lastName = lastName
  broadcastUpdates()
  return true
}

public func set(phone: String) -> Bool {
  phoneNumber = phone
  broadcastUpdates()
  return true
}

public func set(phone: Double) -> Bool {
  phoneNumber = "\(phone)"
  broadcastUpdates()
  return true
}
public override func set(phone: String) -> Bool {
  guard phone.count == 3 else {
  return false
  }
  return super.set(phone: phone)
}
let emergencyContact1 = EmergencyContactCard(emergencyName: "Cold Store Creamery", phoneNumber: "555")
let emergencyContact2 = EmergencyContactCard(emergencyName: "Rifftrax", phoneNumber: "")
print("emergency 1: \(emergencyContact1?.contactInformation() ?? "None")")
print("emergency 21: \(emergencyContact2?.contactInformation() ?? "None")")
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 1 Next: Instruction 2