Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protocol UserType {
- var id: UInt { get set }
- var name: String { get set }
- }
- struct User: UserType {
- var id: UInt
- var name: String
- init(id: UInt, name: String) {
- self.id = id
- self.name = name
- }
- }
- protocol Storage {
- func add(_ user: UserType)
- func delete(_ user: UserType)
- }
- final class RealmStorage: Storage {
- func add(_ user: UserType) {}
- func delete(_ user: UserType) {}
- }
- final class KeychainStorage: Storage {
- func add(_ user: UserType) {}
- func delete(_ user: UserType) {}
- }
- protocol UsersProtocol {
- func didLoad(_ users: [UserType])
- func didRemove(_ user: UserType)
- }
- final class UsersInteractor: UsersProtocol {
- let storage: Storage
- init(storage: Storage) {
- self.storage = storage
- }
- func didLoad(_ users: [UserType]) {
- users.forEach { storage.add($0) }
- }
- func didRemove(_ user: UserType) {
- storage.delete(user)
- }
- }
- let author = User(id: 999, name: "Maxim Vialykh")
- let guest = User(id: 1, name: "Guest")
- let users = [author, guest]
- let usersInteractor = UsersInteractor(storage: RealmStorage())
- usersInteractor.didLoad(users)
- let spyInteractor = UsersInteractor(storage: KeychainStorage())
- spyInteractor.didRemove(guest)
Add Comment
Please, Sign In to add comment