在Android中使用MVP模式时,应该在哪里调用android服务并调用GoogleAPIClient?

Bla*_*sor 11 mvp android design-patterns android-service android-googleapiclient

我试图通过引用此链接在我的android项目中实现MVP模式:https://github.com/jpotts18/android-mvp

我已成功实现了view/presenter/interactor类.我不清楚

  • 在哪里service拨打电话代码?

由于我无法在演示者或者交互器类中获取上下文,因此我无法将service调用放在那里

  • 在哪里实施GoogleApiClient课程?

由于GoogleApiClient还需要运行上下文,因此在没有上下文的情况下也无法在展示器或交互器内实现

小智 7

使用匕首可以更轻松地在Presenter上注入Interactor.试试这个链接(https://github.com/spengilley/AndroidMVPService)

我试图在没有匕首的情况下实现它.但这似乎违反了MVP架构.

从Activity中,我创建了一个Interactor实例.然后使用Interactor创建Presenter实例作为参数之一.

活动

public class SomeActivity extends Activity implements SomeView {
   private SomePresenter presenter;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      SomeInteractor interactor = new SomeInteractorImpl(SomeActivity.this);
      presenter = new SomePresenterImpl(interactor,this);
   }

   @Override
   protected void onStart() {
     super.onStart();
     presenter.startServiceFunction();
   }
Run Code Online (Sandbox Code Playgroud)

主持人

public interface SomePresenter {
   public void startServiceFunction();
}
Run Code Online (Sandbox Code Playgroud)

演示者实施

public class SomePresenterImpl implements SomePresenter {
   private SomeInteractor interactor;
   private SomeView view;
   public SomePresenterImpl(SomeInteractor interactor,SomeView view){
      this.interactor = interactor;
      this.view = view;
   }
   @Override
   public void startServiceFunction() {
      interactor.startServiceFunction();
   }
}
Run Code Online (Sandbox Code Playgroud)

交互器

public interface SomeInteractor {
   public void startServiceFunction();
}
Run Code Online (Sandbox Code Playgroud)

交互式实施

public class SomeInteractorImpl implements SomeInteractor {
   private Context context;

   public SomeInteractorImpl(Context context) {
      this.context = context;
   }

   @Override
   public void startServiceFunction() {
      Intent intent = new Intent(context, SomeService.class);
      context.startService(intent);
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 我不认为答案是对的.如果您遵循Clean Architecture中的指导原则,那么交互者不应该对特定框架有任何了解,并且应该与Android无关:[链接](https://blog.8thlight.com/uncle-bob/2012/08/13/the-clean -architecture.html).因此它应该完全没有任何与Android相关的东西,包括Context类. (3认同)
  • @AutonomousApps如果您在层中划分应用程序,那么Interactors将驻留在域层中.此Layer不应具有任何特定于框架的依赖项,这意味着没有Android依赖项.`Service`启动应该转到DataLayer,在那里你包装这个代码`Intent intent = new Intent(context,SomeService.class); context.startService(intent);`在实现域层接口的类中.然后Interactor将只知道这个接口而不是它的具体实现. (2认同)