使用dagger可以更轻松地在Presenter中注入Interactor。请尝试访问此链接(
https://github.com/spengilley/AndroidMVPService)。
我正在尝试不使用dagger来实现这个目标,但这似乎违反了MVP架构。
从Activity中,我创建了一个Interactor实例。然后创建一个Presenter实例,其中包括Interactor作为其中的一个参数。
Activity
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();
}
Presenter
public interface SomePresenter {
public void startServiceFunction();
}
演示文稿实现
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();
}
}
交互器
public interface SomeInteractor {
public void startServiceFunction();
}
执行者实现
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);
}
}
Context
放在某个地方。否则,你如何与Service
交互?你有什么建议? - AutonomousAppsService
的初始化应该放在 DataLayer 中,在其中将这段代码Intent intent = new Intent(context, SomeService.class);context.startService(intent);
装入实现来自域层的接口的类中。然后,交互器仅会知道此接口而不知道其具体实现。 - koufa