例子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/
例子Architecting Android…The clean way?----代码分析的更多相关文章
- Architecting Android…The clean way?
Architecting Android-The clean way? 原文链接:http://fernandocejas.com/2014/09/03/architecting-android-th ...
- Android 4.2启动代码分析(一)
Android系统启动过程分析 Android系统的框架架构图如下(来自网上): Linux内核启动之后----->就到Android的Init进程 ----->进而启动Android ...
- ironic-conductor与ipa交互clean部分代码分析
clean的动作会在provide和delete阶段才会触发 从代码分析: 对节点执行的node provide/deleted/clean会先发送到ironicclient ironicclient ...
- android recovery 主系统代码分析
阅读完上一篇文章: http://blog.csdn.net/andyhuabing/article/details/9226569 我们已经清楚了如何进入正常模式和Recovery模式已有深刻理解了 ...
- android adb 流程原理代码分析(一)
由于要用到adb的知识,但是对adb啥也不了解,看了下android的代码,adb的源码在system/core/adb下面,然后网上搜下了资料,发现很多大神的源码分析,瞬间信心爆棚,把大神写的博客都 ...
- android recovery 主系统代码分析【转】
本文转载自:http://blog.csdn.net/andyhuabing/article/details/9248713 阅读完上一篇文章: http://blog.csdn.net/andyhu ...
- Android 静态代码分析工具
简评: 作者在文中提到的三个静态代码分析工具不是互相替代的关系,各有各的侧重点,如果有需要完全可以同时使用. 静态代码分析是指无需运行被测代码,仅通过分析或检查源程序的语法.结构.过程.接口等来检查程 ...
- Android代码分析工具lint学习
1 lint简介 1.1 概述 lint是随Android SDK自带的一个静态代码分析工具.它用来对Android工程的源文件进行检查,找出在正确性.安全.性能.可使用性.可访问性及国际化等方面可能 ...
- Android艺术——Bitmap高效加载和缓存代码分析(2)
Bitmap的加载与缓存代码分析: 图片的压缩 比如有一张1024*768像素的图像要被载入内存,然而最终你要用到的图片大小其实只有128*96,那么我们会浪费很大一部分内存,这显然是没有必要的,下面 ...
随机推荐
- 进程间通信:命名管道FIFO(2)
一.命名管道 如果我们想在不相关的进程之间交换数据,可以用FIFO文件来完成这项工作,它通常也被称为命名管道.命名管道是一种特殊类型的文件,它在文件系统中以文件名的形式存在,但是它的行为却和我们已经见 ...
- 解决python中文编码错误问题
对于初学者而言,编码问题或许还没有没重视起来,但是编码问题是中文开发者必须面对的.今天来看下python开发中如何解决编码问题.注意:本篇讲的是最常见的一种编码问题,其他编码问题,如json函数引起的 ...
- UVALive - 6869 Repeated Substrings 后缀数组
题目链接: http://acm.hust.edu.cn/vjudge/problem/113725 Repeated Substrings Time Limit: 3000MS 样例 sample ...
- 第三章——供机器读取的数据(XML)
本书使用的文件.代码:https://github.com/huangtao36/data_wrangling 机器可读(machine readable)文件格式: 1.逗号分隔值(Comma-Se ...
- <Android>spinner/AutoCompleteTextView绑定适配器
position = (Spinner)findViewById(R.id.position); String[] str = {"CEO","CFO",&qu ...
- [洛谷P4900]食堂
题目大意:$n(n\leqslant10^6)$组询问,每组询问给出$l,r(l,r\leqslant10^6)$,求($\{\dfrac ij\}$表示$\dfrac ij$的小数部分): $$\s ...
- [洛谷P5166]xtq的口令
题目大意:给出一张有向图,保证任何时候边都是从编号大的向编号小连.两个操作: $1\;l\;r:$表示若编号在区间$[l,r]$内的点被染色了,问至少还需要染多少个点才可以使得整张图被染色.一个点会被 ...
- [洛谷P4070][SDOI2016]生成魔咒
题目大意:有一个字符串,每次在末尾加入一个字符,问当前共有多少个本质不同的字串 题解:$SAM$,就是问插入这个字符后,多了多少个字串,就是当前这个点的$Right$数组大小. 卡点:无 C++ Co ...
- [Leetcode] combination sum ii 组合之和
Given a collection of candidate numbers ( C ) and a target number ( T), find all unique combinations ...
- BZOJ1006 神奇的国度 【弦图染色——最大势算法MCS】
1006: [HNOI2008]神奇的国度 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 4146 Solved: 1916 [Submit][S ...