Skip to content

Instantly share code, notes, and snippets.

@joanmolinas
Last active March 8, 2018 19:10
Show Gist options
  • Save joanmolinas/4b7791b0c739f335ba6fccba341c7377 to your computer and use it in GitHub Desktop.
Save joanmolinas/4b7791b0c739f335ba6fccba341c7377 to your computer and use it in GitHub Desktop.
Blog.firstSolidPrinciples
class UserInformationHandler {
// MARK: - Properties
let requester: UserInformationRequesteable
let parser: UserInformationParseable
let storer: UserInformationStoreable
// MARK - Life cycle
init(requester: UserInformationRequesteable,
parser: UserInformationParseable,
storer: UserInformationStoreable) {
self.requester = requester
self.parser = parser
self.storer = storer
}
func handle() {
let data = requester.request()
let user = parser.parse(data: data)
storer.store(user: user)
}
}
protocol UserInformationRequesteable {
func request() -> Data
}
class UserInformationRequester: UserInformationRequesteable {
func request() -> Data {
// Interaction with the api
// Synchronous call for simplicity
}
}
protocol UserInformationParseable {
func parse(data: Data)
}
class UserInformationParser: UserInformationParseable {
func parse(data: Data) {
// parse data an create a dictionary
}
}
protocol UserInformationStoreable {
func store(user: [String: Any])
}
class UserInformationStorer: UserInformationStoreable {
func store(user: [String: Any]) {
// store on your local database, the information of the user
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment