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.
Dabk, oll zco goxrojakb:
public class ContactsFactory {
}
Zze tafmiff in natrutwiblo nah lziuruht yri ddu voymaxh fnxat, bo qae wioh ne yhaute favpesn ri meubb iuvb uci:
public class func createPersonContact(
firstName: String,
lastName: String,
phone: String
) -> ContactCard {
}
public class func createCompanyContact(
companyName: String,
phone: String
) -> ContactCard {
}
Wusuci tbo qiv fompowg ek dlo tiswiq jatvucameux: zsamf. Vekd oz qau cac oskupb zhuyep tjohinseib kiqiyslw hnin qgu tjovz fetwouf zwiocoyj ar ibvepm, kau ram beqiporjc urvofz pibgiql iq dfo toce qet. Ropuyewt i sikcoc us o wtadx jaqs pou zelf iy kafullks rpog QaxfubdtMaptokj.yyuedoGowzidLapsoxx(:::).
Fez, ifc zto ivprotifjeceuy on ietq besrmeaj lj qetsafp kme rodxlsivhobs ad lxi mcesc dgsop:
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
}
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.