[英]How do I use Repository pattern and Interactor pattern in a Model View Presenter pattern?
I am developing an application with the help of Model View Presenter pattern.我正在 Model View Presenter 模式的帮助下开发应用程序。
I make use of Retrofit and so I have a ApiClient and ApiInterface with endpoints.我使用 Retrofit ,所以我有一个带有端点的 ApiClient 和 ApiInterface 。 I implement the interface in a RemoteDataSource class which I call in the Repository class.我在Repository class 中调用的RemoteDataSource class 中实现了接口。
My questions is - how do I make use of an Interactor class to make the repository communicate with the Presenter?我的问题是 - 如何使用交互器 class 使存储库与演示者通信?
Here is my code until now:到目前为止,这是我的代码:
ApiInterface API接口
public interface ApiInterface {
@GET("?")
Call<ArrayList<Movie>> getMoviesByTitle(@Query("t") String title,@Query("apiKey") String apiKey);
}
RemoteDataSource class远程数据源 class
private static MovieRemoteDataSource instance;
private final ApiInterface service;
public MovieRemoteDataSource(ApiInterface movieApi) {
service = ApiClient.createService(ApiInterface.class);
}
public static MovieRemoteDataSource getInstance(ApiInterface movieApi) {
if (instance == null) {
instance = new MovieRemoteDataSource(movieApi);
}
return instance;
}
@Override
public void getMovies(String title, String apiKey, final LoadMovieCallBack callback) {
service.getMoviesByTitle(title,apiKey).enqueue(new Callback<ArrayList<Movie>>() {
@Override
public void onResponse(Call<ArrayList<Movie>> call, Response<ArrayList<Movie>> response) {
ArrayList<Movie> movies = response.body();// != null ? //response.body().getTitle() : null;
if (movies != null && !movies.isEmpty()) {
callback.onMoviesLoaded(movies);
} else {
callback.onDataNotAvailable();
}
}
@Override
public void onFailure(Call<ArrayList<Movie>> call, Throwable t) {
callback.onError();
}
});
}
DataSource interface with a callback带有回调的 DataSource 接口
public interface MovieDataSource {
interface LoadMovieCallBack{
void onMoviesLoaded(ArrayList<Movie> movies);
void onDataNotAvailable();
void onError();
}
void getMovies(String title, String apiKey,LoadMovieCallBack callback);
}
Repository存储库
private MovieRemoteDataSource movieRemoteDataSource;
public MoviesRepository() {//ApiInterface movieApi) {
//this.service = ApiClient.createService(ApiInterface.class);
}
public static MoviesRepository getInstance(ApiInterface service) {
if (instance == null) {
instance = new MoviesRepository();
}
return instance;
}
public void getMovies(String title, String apiKey ) {
movieRemoteDataSource.getMovies(title,apiKey,this);
}
In MoviesRepository
you should declare a function with Callback
.在MoviesRepository
,您应该使用Callback
声明 function 。 Your Presenter
should implement MovieDataSource.LoadMovieCallBack
and pass it when you call MoviesRepository
您的Presenter
应该实现MovieDataSource.LoadMovieCallBack
并在您调用MoviesRepository
时传递它
public void getMovies(String title, String apiKey,MovieDataSource.LoadMovieCallBack callback) {
movieRemoteDataSource.getMovies(title,apiKey,callback);
}
Here is Google MVP
already done for todo app sample, you can refer it. 这是 Google MVP
已经为 todo 应用示例完成的,您可以参考它。 But now it deprecated because Google recommends MVVM
但现在它已被弃用,因为 Google 推荐MVVM
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.