Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Adapter pattern
- - 써드파티 라이브러리에서 제공하는 것을 변형해서 사용하고 싶을 때
- - legacy object
- ```swift
- public struct GoogleUser {
- public var email: String
- public var password: String
- public var token: String
- }
- public class GoogleAuthenticator {
- public func login(email: String,
- password: String,
- completion: @escaping (GoogleUser?, Error?) -> Void) {
- let token = "special-token-value"
- let user = GoogleUser(email: email,
- password: password,
- token: token)
- completion(user, nil)
- }
- }
- ```
- - 내가 사용하고 싶은 것
- ```swift
- public struct User {
- public let email: String
- public let password: String
- }
- public struct Token {
- public let value: String
- }
- ```
- - new protocol
- ```swift
- public protocol AuthenticationService {
- func login(email: String,
- password: String,
- success: @escaping (User, Token) -> Void,
- failure: @escaping (Error?) -> Void)
- }
- ```
- - adapter
- ```swift
- public class GoogleAuthenticatorAdapter: AuthenticationService {
- private var authenticator = GoogleAuthenticator()
- public func login(email: String, password: String, success: @escaping (User, Token) -> Void, failure: @escaping (Error?) -> Void) {
- authenticator.login(email: email, password: password) { (googleUser, error) in
- guard let googleUser = googleUser else {
- failure(error)
- return
- }
- let user = User(email: googleUser.email,
- password: googleUser.password)
- let token = Token(value: googleUser.token)
- success(user, token)
- }
- }
- }
- ```
Add Comment
Please, Sign In to add comment