【发布时间】:2016-06-13 19:17:45
【问题描述】:
我想让我的子组件成为单例,这样我也可以让 Login Presenter 成为单例。这可能吗?
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
LoginComponent getLoginComponent();
}
@Singleton
@Subcomponent(modules = LoginModule.class)
public interface LoginComponent {
}
public class LoginComponent {
@Singleton
LoginPresenter getLoginPresenter();
}
【问题讨论】:
-
所以让你的
LoginPresenter成为单身人士。为什么必须从子组件中提供它? -
对我来说,子组件代表一个功能,我想将我所有的对象创建保留在那里。
-
组件和子组件是用来封装绑定图的对象。由这些绑定创建的实例可以选择具有与组件的生命周期相匹配的生命周期,并与范围注释定义的逻辑概念相关联。因此,您应该使用组件和子组件来建模相关的生命周期(例如 Android 或 Singleton 中的 Application/Singleton、Activity 等,服务器中的 Request),而不是功能。功能级组合应该使用模块来完成,就像您似乎已经完成的那样。
-
如果我使用模块进行功能级组合,那么我的 AppComponent 会变得越来越大,为了组织起见,我想避免这种情况。但我想你是对的,两个不同的组件不能有相同的生命周期。我希望这是可能的。