简体   繁体   English

如何在 Model View Presenter 模式中使用存储库模式和交互器模式?

[英]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.

相关问题 如何使用 android 中的 MVP 模式从我的交互器启动服务? - How do I start a service from my Interactor using the MVP pattern in android? 对Java Swing应用程序使用Model-View-Presenter设计模式 - Using the Model-View-Presenter design pattern for a Java Swing application GWT中的MVP模式:如何在Presenter中初始化Model / View和处理服务器请求? - MVP Pattern in GWT: How to initialize Model/View and handle server requests in the Presenter? 如何在另一个模式中再次使用声明的正则表达式模式? - How do I use declared regex Pattern again in another Pattern? 具有存储库模式的域模型 - Domain Model with Repository Pattern 如何在 Controller、服务和存储库模式中使用 DTO - How to use DTOs in the Controller, Service and Repository pattern 如何使用一种正则表达式模式使String成为Java中的另一种正则表达式模式? - How do I use one regex pattern to make a String become another regex pattern in Java? 如何在此MVC模式程序中实现Observer模式 - How do I implement the Observer pattern in this MVC pattern program 存储库模式:存储库可以使用其他存储库吗? - Repository pattern: Can a repository use other repositories? 在Java规范模式库图案 - repository pattern with specification pattern in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM