Skip to content

Instantly share code, notes, and snippets.

@aainaj
Created February 12, 2019 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aainaj/c66e765a74ff642ff380cda31a3f15eb to your computer and use it in GitHub Desktop.
Save aainaj/c66e765a74ff642ff380cda31a3f15eb to your computer and use it in GitHub Desktop.
Dependency Inversion Example
protocol Storable {}
struct User: Storable {
let name: String
let identifier: String
}
protocol UserTransaction {
func add(user: User)
func edit(user: User)
func delete(identifier: String)
}
protocol DataStore {
func insert<T>(_ data: T) where T: Storable
func update<T>(_ data: T) where T: Storable
func delete(identifier: String)
}
final class DefaultUserTransaction: UserTransaction {
private let dataStore: DataStore
init(dataStore: DataStore) {
self.dataStore = dataStore
}
func add(user: User) {
dataStore.insert(user)
}
func edit(user: User) {
dataStore.update(user)
}
func delete(identifier: String) {
dataStore.delete(identifier: identifier)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment