Last active
September 23, 2018 10:27
-
-
Save devxoul/31b46dd63bd543acb5ff0e69ac45a700 to your computer and use it in GitHub Desktop.
Which do you think is better design? (in the perspective of scalability, consistency, etc.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////////////////////////////////////////// | |
// Usage // | |
//////////////////////////////////////////////////////////////////////////////// | |
struct JoinForm { | |
var emailField: FormField | |
var usernameField: FormField | |
init(email: String?) { | |
self.emailField = FormField(value: email) | |
self.usernameField = FormField(value: parseUsernameFromEmail(email)) | |
} | |
init(socialAccountProfile: SocialAccountProfile) { | |
// autofill value from social account profile | |
self.emailField = FormField(value: socialAccountProfile.email) | |
self.usernameField = FormField(value: socialAccountProfile.username ?? parseUsernameFromEmail(socialAccountProfile.email)) | |
} | |
} | |
let socialAccountProfile: SocialAccountProfile = FacebookCredential.login() // can be replaced with TwitterCredential.login(), .... | |
let joinForm = JoinForm(socialAccountProfile: socialAccountProfile) | |
let joinFormViewController = JoinFormViewController(joinForm: joinForm) | |
present(joinFormViewController, animated: true, completion: nil) | |
//////////////////////////////////////////////////////////////////////////////// | |
// Candidate A. 1 struct and extend with initializer // | |
//////////////////////////////////////////////////////////////////////////////// | |
// SocialAccountProfile.swift | |
struct SocialAccountProfile { | |
let email: String? | |
let username: String? | |
} | |
// FacebookCredential.swift | |
extension SocialAccountProfile { | |
init(facebookUser: [String: Any]) { | |
self.email = facebookUser["email"] as? String | |
self.username = facebookUser["username"] as? String | |
} | |
} | |
// TwitterCredential.swift | |
extension SocialAccountProfile { | |
init(twitterUser: TWTRUser) { | |
self.email = nil | |
self.username = twitterUser.screenName | |
} | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
// Candadate B. 1 protocol and N structs // | |
//////////////////////////////////////////////////////////////////////////////// | |
// SocialAccountProfile.swift | |
protocol SocialAccountProfile { | |
var email: String? { get } | |
var username: String? { get } | |
} | |
// FacebookCredential.swift | |
struct FacebookProfile: SocialAccountProfile { | |
let email: String? | |
let username: String? | |
init(facebookUser: [String: Any]) { | |
self.email = facebookUser["email"] as? String | |
self.username = facebookUser["username"] as? String | |
} | |
} | |
// TwitterCredential.swift | |
struct TwitterProfile: SocialAccountProfile { | |
let email: String? | |
let username: String? | |
init(twitterUser: TWTRUser) { | |
self.email = nil | |
self.username = twitterUser.screenName | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment