Created
February 12, 2019 04:01
-
-
Save aainaj/c66e765a74ff642ff380cda31a3f15eb to your computer and use it in GitHub Desktop.
Dependency Inversion Example
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
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