例子Architecting Android…The clean way?----代码分析
/**
* A component whose lifetime is the life of the application.
*/
@Singleton // Constraints this component to one-per-application or unscoped bindings.
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(BaseActivity baseActivity);
//Exposed to sub-graphs.
Context context();
ThreadExecutor threadExecutor();
PostExecutionThread postExecutionThread();
UserRepository userRepository();
}
/**
* Dagger module that provides objects which will live during the application lifecycle.
*/
@Module
public class ApplicationModule {
private final AndroidApplication application;
public ApplicationModule(AndroidApplication application) {
this.application = application;
}
@Provides @Singleton Context provideApplicationContext() {
return this.application;
}
@Provides @Singleton Navigator provideNavigator() {
return new Navigator();
}
@Provides @Singleton ThreadExecutor provideThreadExecutor(JobExecutor jobExecutor) {
return jobExecutor;
}
@Provides @Singleton PostExecutionThread providePostExecutionThread(UIThread uiThread) {
return uiThread;
}
@Provides @Singleton UserCache provideUserCache(UserCacheImpl userCache) {
return userCache;
}
@Provides @Singleton UserRepository provideUserRepository(UserDataRepository userDataRepository) {
return userDataRepository;
}
}
/**
* Android Main Application
*/
public class AndroidApplication extends Application {
private ApplicationComponent applicationComponent;
@Override public void onCreate() {
super.onCreate();
this.initializeInjector();
}
private void initializeInjector() {
this.applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
}
public ApplicationComponent getApplicationComponent() {
return this.applicationComponent;
}
}
,
/**
* A scope {@link com.fernandocejas.android10.sample.presentation.internal.di.PerActivity} component.
* Injects user specific Fragments.
*/
@PerActivity
@Component(dependencies = ApplicationComponent.class, modules = {ActivityModule.class, UserModule.class})
public interface UserComponent extends ActivityComponent {
void inject(UserListFragment userListFragment);
void inject(UserDetailsFragment userDetailsFragment);
}
/**
* A base component upon which fragment‘s components may depend.
* Activity-level components should extend this component.
*
* Subtypes of ActivityComponent should be decorated with annotation:
* {@link com.fernandocejas.android10.sample.presentation.internal.di.PerActivity}
*/
@PerActivity
@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {
//Exposed to sub-graphs.
Activity activity();
}
/**
* A module to wrap the Activity state and expose it to the graph.
*/
@Module
public class ActivityModule {
private final Activity activity;
public ActivityModule(Activity activity) {
this.activity = activity;
}
/**
* Expose the activity to dependents in the graph.
*/
@Provides @PerActivity Activity activity() {
return this.activity;
}
}
/**
* Dagger module that provides user related collaborators.
*/
@Module
public class UserModule {
private int userId = -1;
public UserModule() {}
public UserModule(int userId) {
this.userId = userId;
}
@Provides @PerActivity @Named("userList") UseCase provideGetUserListUseCase(
GetUserListUseCase getUserListUseCase) {
return getUserListUseCase;
}
@Provides @PerActivity @Named("userDetails") UseCase provideGetUserDetailsUseCase(
UserRepository userRepository, ThreadExecutor threadExecutor,
PostExecutionThread postExecutionThread) {
return new GetUserDetailsUseCase(userId, userRepository, threadExecutor, postExecutionThread);
}
}
@Override public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof UserListListener) {
this.userListListener = (UserListListener) activity;
}
}
@Override public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
this.initialize();
this.loadUserList();
}
private void initialize() {
this.getComponent(UserComponent.class).inject(this);
this.userListPresenter.setView(this);
}
/**
* Loads all users.
*/
private void loadUserList() {
this.userListPresenter.initialize();
}
@Inject
public UserListPresenter(@Named("userList") UseCase getUserListUserCase, UserModelDataMapper userModelDataMapper) {
this.getUserListUseCase = getUserListUserCase;
this.userModelDataMapper = userModelDataMapper;
}
/**
* Initializes the presenter by start retrieving the user list.
*/
public void initialize() {
this.loadUserList();
}
/**
* Loads all users.
*/
private void loadUserList() {
this.hideViewRetry();
this.showViewLoading();
this.getUserList();
}
private void getUserList() {
this.getUserListUseCase.execute(this); //这里使用到了domain层上的类,domain层提供用例类。一个用例类表示一个表示一个业务,在这里表示获取用户列表。
}
参考资料:http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。