janos
07/24/2018, 5:47 PMclass Client {
fun addHeader(context: Context): Client {
// adds request headers from the context
return this
}
fun getMethod(name: String) {
println(name)
}
}
typealias Context = Any
private val HttpContext = "some-stuff"
class ServiceFooFactory {
private val baseClinet = Client()
fun createServiceFoo(context: Context): ServiceFoo {
return ServiceFoo(baseClinet.addHeader(context))
}
class ServiceFoo(private val enhancedClient: Client) {
fun hello(name: String) {
enhancedClient.getMethod(name)
}
}
}
fun main(args: Array<String>) {
val factory = ServiceFooFactory()
factory.createServiceFoo(HttpContext).hello("Bar")
}
Which is working fine.
Although I am asking myself if there is a neat way to do a DSL like thing.
To achieve something like this:
factory.withContext { context ->
hello("Bar")
}