Last active
November 23, 2021 19:53
-
-
Save cjnevin/142ef5841434e91e24d1322b0bc56213 to your computer and use it in GitHub Desktop.
Concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Presenter<T> { | |
private var view: T? | |
func attachView(_ view: T) { | |
self.view = view | |
} | |
func detachView() { | |
self.view = nil | |
} | |
} | |
struct UserDTO { | |
let firstName: String | |
let lastName: String | |
} | |
protocol DataStore { | |
func getUser() -> UserDTO | |
} | |
class UserDataStore: DataStore { | |
func getUser() -> UserDTO { | |
return UserDTO(firstName: "Mary", lastName: "Jane") | |
} | |
} | |
class UserInteractor<T: DataStore> { | |
struct User { | |
let firstName: String | |
let lastName: String | |
} | |
private let dataStore: T | |
init(dataStore: T) { | |
self.dataStore = dataStore | |
} | |
func getUser() -> User { | |
let dtoUser = dataStore.getUser() | |
return User(firstName: dtoUser.firstName, lastName: dtoUser.lastName) | |
} | |
} | |
protocol UserView { | |
func setFirstName(_ firstName: String) | |
func setLastName(_ lastName: String) | |
} | |
class UserPresenter<T: UserView, U: UserDataStore>: Presenter<T> { | |
private let interactor: UserInteractor<U> | |
init(interactor: UserInteractor<U>) { | |
self.interactor = interactor | |
} | |
override func attachView(_ view: T) { | |
super.attachView(view) | |
let user = interactor.getUser() | |
view.setFirstName(user.firstName) | |
view.setLastName(user.lastName) | |
} | |
} | |
class UserViewController: UIViewController, UserView { | |
func setFirstName(_ firstName: String) { | |
debugPrint("First name = \(firstName)") | |
} | |
func setLastName(_ lastName: String) { | |
debugPrint("Last name = \(lastName)") | |
} | |
} | |
let myInteractor = UserInteractor<UserDataStore>(dataStore: UserDataStore()) | |
let myPresenter = UserPresenter<UserViewController, UserDataStore>(interactor: myInteractor) | |
myPresenter.attachView(UserViewController(nibName: nil, bundle: nil)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment