Guest User

Untitled

a guest
Dec 12th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. # Adapter pattern
  2. - 써드파티 라이브러리에서 제공하는 것을 변형해서 사용하고 싶을 때
  3. - legacy object
  4. ```swift
  5. public struct GoogleUser {
  6. public var email: String
  7. public var password: String
  8. public var token: String
  9. }
  10.  
  11. public class GoogleAuthenticator {
  12.  
  13. public func login(email: String,
  14. password: String,
  15. completion: @escaping (GoogleUser?, Error?) -> Void) {
  16. let token = "special-token-value"
  17. let user = GoogleUser(email: email,
  18. password: password,
  19. token: token)
  20. completion(user, nil)
  21. }
  22. }
  23. ```
  24.  
  25.  
  26. - 내가 사용하고 싶은 것
  27. ```swift
  28. public struct User {
  29. public let email: String
  30. public let password: String
  31. }
  32.  
  33. public struct Token {
  34. public let value: String
  35. }
  36. ```
  37.  
  38. - new protocol
  39.  
  40. ```swift
  41. public protocol AuthenticationService {
  42. func login(email: String,
  43. password: String,
  44. success: @escaping (User, Token) -> Void,
  45. failure: @escaping (Error?) -> Void)
  46. }
  47.  
  48. ```
  49.  
  50. - adapter
  51.  
  52. ```swift
  53. public class GoogleAuthenticatorAdapter: AuthenticationService {
  54.  
  55. private var authenticator = GoogleAuthenticator()
  56.  
  57. public func login(email: String, password: String, success: @escaping (User, Token) -> Void, failure: @escaping (Error?) -> Void) {
  58. authenticator.login(email: email, password: password) { (googleUser, error) in
  59.  
  60. guard let googleUser = googleUser else {
  61. failure(error)
  62. return
  63. }
  64.  
  65. let user = User(email: googleUser.email,
  66. password: googleUser.password)
  67. let token = Token(value: googleUser.token)
  68. success(user, token)
  69. }
  70. }
  71. }
  72.  
  73. ```
Add Comment
Please, Sign In to add comment