Skip to content

Instantly share code, notes, and snippets.

@caseykulm
Last active September 6, 2017 18:20
Show Gist options
  • Save caseykulm/b89218ec2be0fc8dd06c8d26354a0163 to your computer and use it in GitHub Desktop.
Save caseykulm/b89218ec2be0fc8dd06c8d26354a0163 to your computer and use it in GitHub Desktop.
Abstract Factory for Retro APIs
interface RetroDogApi {
@GET("/dogs/{doggoName}")
Flowable<Dog> getDoggo(@Path("doggoName") String doggoName);
}
interface DogApi {
Flowable<Dog> getDoggo(String doggoName);
}
interface UrlInfoApi {
Flowable<String> getBaseUrl()
}
interface RetroDogApiFactory {
fun createRetroDogApi(baseUrl: String): RetroDogApi
}
class RealRetroDogApiFactory: RetroDogApiFactory {
fun createRetroDogApi(baseUrl: String) {
val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.build()
return retrofit.create(RetroDogApi::class.java)
}
}
class DogClient(val retroDogApiFactory: DogApiFactory, val urlInfoApi: UrlInfoApi): DogApi {
Flowable<Dog> getDoggo(String doggoName) {
return urlInfoApi.getBaseUrl()
.flatMap((baseUrl) -> {
return dogApiFactory.createRetroDogApi(baseUrl)
})
.flatMap((retroDogApi) -> {
return retroDogApi.getDog(doggoName)
})
}
}
class UseIt {
fun use() {
val retroDogApiFactory = RealRetroDogApiFactory()
val urlInfoApi = UrlInfoClient() // need to create this implementation
val dogApi = DogClient(retroDogApiFactory, urlInfoApi)
dogApi.getDoggo("fido")
.subscribeOn(...)
.observeOn(...)
.subscribe(...)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment