Predict the output
Which pattern does this code use?
class TimingRepository(private val inner: UserRepository) {
suspend fun user(id: String): User {
val start = System.nanoTime()
return inner.user(id).also {
log("user($id) took ${(System.nanoTime() - start) / 1_000_000} ms")
}
}
}Hint 1 of 1
What does it do with the inner object?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Decorator
Correct. It wraps another object with the same job and adds behaviour around it.
Why not the others?
- B Factory. It doesn't create objects for callers.
- C Observer. Nothing subscribes to changes.
- D Singleton. Nothing limits the number of instances.