Last active
September 6, 2017 18:20
-
-
Save caseykulm/b89218ec2be0fc8dd06c8d26354a0163 to your computer and use it in GitHub Desktop.
Abstract Factory for Retro APIs
This file contains hidden or 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 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