Last active
November 29, 2022 16:31
-
-
Save vinaysshenoy/31ede0981a9fa88fff84b16c51352513 to your computer and use it in GitHub Desktop.
Onion layers
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
interface Provider<T, U> { | |
fun provide(params: U?): T | |
} | |
class DatabaseProvider: Provider<User, string> { | |
override fun provide(id: string?): User { | |
// Query DB here | |
} | |
} | |
class CachingProvider<T, U>: Provider<T, U>( | |
private val source: Provider<T, U> | |
) { | |
private cache: Map<U, T> | |
override fun provide(params: U?): T { | |
// Check and update cache | |
} | |
} | |
class FetchUser(private val provider: Provider<User, string>) { | |
} | |
val provider = CachingProvider(DatabaseProvider()) | |
val fetchUser = FetchUser(provider) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment