Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 57 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. protocol UserType {
  2.     var id: UInt { get set }
  3.     var name: String { get set }
  4. }
  5.  
  6. struct User: UserType {
  7.    
  8.     var id: UInt
  9.     var name: String
  10.    
  11.     init(id: UInt, name: String) {
  12.         self.id = id
  13.         self.name = name
  14.     }
  15. }
  16.  
  17. protocol Storage {
  18.     func add(_ user: UserType)
  19.     func delete(_ user: UserType)
  20. }
  21.  
  22. final class RealmStorage: Storage {
  23.    
  24.     func add(_ user: UserType) {}
  25.    
  26.     func delete(_ user: UserType) {}
  27.    
  28. }
  29.  
  30. final class KeychainStorage: Storage {
  31.    
  32.     func add(_ user: UserType) {}
  33.    
  34.     func delete(_ user: UserType) {}
  35.    
  36. }
  37.  
  38. protocol UsersProtocol {
  39.     func didLoad(_ users: [UserType])
  40.     func didRemove(_ user: UserType)
  41. }
  42.  
  43. final class UsersInteractor: UsersProtocol {
  44.    
  45.     let storage: Storage
  46.    
  47.     init(storage: Storage) {
  48.         self.storage = storage
  49.     }
  50.    
  51.     func didLoad(_ users: [UserType]) {
  52.         users.forEach { storage.add($0) }
  53.     }
  54.    
  55.     func didRemove(_ user: UserType) {
  56.         storage.delete(user)
  57.     }
  58.    
  59. }
  60.  
  61. let author = User(id: 999, name: "Maxim Vialykh")
  62. let guest = User(id: 1, name: "Guest")
  63. let users = [author, guest]
  64.  
  65. let usersInteractor = UsersInteractor(storage: RealmStorage())
  66. usersInteractor.didLoad(users)
  67.  
  68. let spyInteractor = UsersInteractor(storage: KeychainStorage())
  69. spyInteractor.didRemove(guest)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top