The following code presents an example of protocol conformance with associated type usage:
enum MobileAppUserType: MobileAppUserProtocol {
case admin
case endUser
func greet(name: String) -> String {
switch self {
case .admin:
return "Welcome (name) - You are Admin"
case .endUser:
return "Welcome (name)!"
}
}
func login(username: String, password: String) -> Bool {
return true
}
func listSelectedModules() -> [String] {
return ["Accounting", "CRM"]
}
}
Then we can create a new mobile user as follows:
let mobileUser: MobileAppUserType = MobileAppUserType.admin
mobileUser.logout(userName: "Su Tamina")
mobileUser.listSelectedModules()
POP minimizes the inheritance and subclassing necessities by enabling us to conform to protocols and extend them with default implementations.