S
S
sam_u2018-04-09 05:58:07
Java
sam_u, 2018-04-09 05:58:07

How to choose from two interfaces the one you need according to the condition?

Good afternoon!
Direct your thoughts to the right side, I can not solve the problem.
The application has 2 interfaces Api and ApiDemo. As the name implies, one works with a working server, the other with a demo one. When the application is launched, the user can test it in demo mode (there is a separate button for this).
I use Retrofit 2 to work with the server, and the worker api object is created like this
Api = new Retrofit.Builder()
.baseUrl(serverAddress + "/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build().create(Api.Class);
App.getApi
().auth(userName, password) is called.
A demo api can be created similarly in theory.
I save the "demo mode" flag in SharedPreference.
My problem is that I don't understand how to connect 2 different interfaces into one?
I want that when calling App.getApi () - I immediately return the desired interface (working or demo).
Otherwise, you will have to cut the heap when, and add an if-else condition everywhere with a check of the current mode of operation ... but you don’t want to.
Maybe I made a mistake with the architecture initially?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-04-09
@sam_u

In theory, the API is what the interface is for, that for sale and test should not differ externally. That is, the interface itself must be one. And just baseUrl should differ.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question