java 存储库模式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 存储库模式相关的知识,希望对你有一定的参考价值。

public class NetworkRequest implements NetworkRequestSource{
  
  //TODO("not implemented") this class should be Singleton
  
  @override
  public void getRemoteUser(OnRemoteUserCallback onRemoteUserCallback){
    //code that makes WebService Request to get the user from the server.
    //if we got the user object then we send it through the Callback
    onRemoteUserCallback.onUserExists(//user that we got from the servcer 
                                      user);
    //if we got error from the server 
     onRemoteUserCallback.onUserError(errorMessage)
  }
}
interface NetworkRequestSource{
  public void getRemoteUser(OnRemoteUserCallback onRemoteUserCallback);
  
  interface OnRemoteUserCallback{
    
    public void onUserExists(User user);
    public void onUserError(String msg);
  }
}

//"Source" in object means it's an Interface object from any class that implments this Interface

public class DataRepository implements DataSource {
  
  //this is the networking Interfcae object that is resposible for handling all the WebService APIs
  NetworkRequestSource networkRequest;
  
  //this is the local Class object that is resposible for handling all the local database(SQL, etc..) 
  LocalRequestSource localRequest
  
  @override
   public void getCurrentUser(OnUserCallback userCallback){
    User user= localRequest.getUser();
    if (user==null){
      networkRequest.getRemoteUser(new OnRemoteUserCallback(){
        @override
        public void onUserExists(User user){
          userCallback.onUserExists(user)
          localRequest.cacheUser(user)
          
    
           }
        @override
        public void onUserError (String msg){
           userCallback.onUserError(msg)
    
            }
      });
    }else{
      userCallback.onUserExists(user)
    }
     
     
   }
   
   
   private DataRepository(NetworkRequestSource networkRequest,LocalRequestSource localRequest){
     this.networkRequest=networkRequest
     this.localRequest=localRequest
     
     
   }
   
   private static DataRepository INSTANCE =null;
   public static DataRepository newInstance(NetworkRequestSource networkRequest,LocalRequestSource localRequest){
            if (INSTANCE == null) {
            INSTANCE = new  DataRepository( networkRequest, localRequest);
        }
        return INSTANCE;
   }
  
}
interface DataSource {
  // this is the interface which any presenter in MVP can communicate with DataRepository with 
  // eg. getting the user data from local database
  // eg. request some webservice API
  public void getCurrentUser(OnUserCallback userCallback);
  
  //check the example below
  interface OnUserCallback{
    public void onUserExists(User user);
    public void onUserError (String msg);
  }
}

// this is an example of how the callback implemented in the presenter.
mDataRepository.getCurrentUser(new OnUserCallback(){
  @override
  public void onUserExists(User user){
    
    
    
  }
   @override
  public void onUserError (String msg){
    
  }
  
} )

以上是关于java 存储库模式的主要内容,如果未能解决你的问题,请参考以下文章

java 存储库模式的Android LocalDataSource示例。

java 存储库模式的Android Repository类示例。

存储库模式的替代方案?

具有存储库模式的 Laravel 策略

如何在存储库模式中为同一接口使用两个数据存储?

存储库模式 - 如何正确处理 JOIN 和复杂查询?