Presention层:
 
整个应用启动的时候,就执行依赖的初始化。编译项目之后,Dagger依赖框架使用ApplicationComponent生成一个DaggerApplicationCOmponent。
 
1. 首先进行依赖的生成
在Application中,调用initializeInjector()就会促使Dagger框架进行依赖生成。
 
ApplicationComponent
对其它Component提供Context,ThreadExecutor,PostExecutionThread,UserRepository依赖
/**
* 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();
}
ApplicationModule
/**
* 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;
}
}
在这个类中,提供了生成依赖的方法。
需要参数的provide方法,参数也是通过Dagger依赖框架生成的。
参数JobExecutor(data模块),UIThread(presentation模块),UserCacheImpl(data模块),UserDataRepository(data模块)生成的方式,是Dagger依赖框架调用相应类的构造方法实现的,而不是通过provide实现的。
比如
JobExecutor(data模块):
public class JobExecutor implements ThreadExecutor {
  @Inject
  public JobExecutor() {
    this.workQueue = new LinkedBlockingQueue<>();
    this.threadFactory = new JobThreadFactory();
    this.threadPoolExecutor = new ThreadPoolExecutor(INITIAL_POOL_SIZE, MAX_POOL_SIZE,
        KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, this.workQueue, this.threadFactory);
  }
...
}
属于data模块的:参数JobExecutor(data模块),UserCacheImpl(data模块),UserDataRepository(data模块)
 
Application:
/**
* 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;
}
}
2.MainActivity实现
 
使用ButterKnife对View对象进行注入,从而无需写findViewById。
 
2.1继承BaseActivity,BaseActivity的实现
 
A.
声明了一个带有Inject注解的成员Navigator
在onCreate方法中,将Navigator依赖注入到当前实例。
 
B.
提供三个protected方法,一个是用来添加fragment到当前界面的某个位置;两个是用来进行依赖注入的。
 
2.2 MainActivity执行流程
用户点击R.id.btn_LoadData Button,就会跳转到界面UserListActivity.
 
3.UserListActivity实现
 
A.
界面加载,使用UserListFragment。
 
B.
在onCreate方法中,执行UserComponent包含的依赖初始化。
  private void initializeInjector() {
    this.userComponent = DaggerUserComponent.builder()
        .applicationComponent(getApplicationComponent())
        .activityModule(getActivityModule())
        .build();
  }
 
UserComponent生成的依赖,被注入到UserListFragment,UserDetailsFragment。UserListFragment,UserDetailsFragment调用UserComponent实例的inject方法。
UserComponent如下:
可以注入到UserListFragment,UserDetailsFragment的依赖是GetUserListUseCase,GetUserDetailsUseCase。
,
/**
* 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);
}
}
4.UserListFragment的实现
 
A.
UserListFragment实现UserListView接口,用来表示View层。
B.
UserListFragment跟宿主Activity的通信,通过UserListFragment声明的接口UserListListener来实现。
@Override public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof UserListListener) {
this.userListListener = (UserListListener) activity;
}
}
 
C.
当宿主Activity创建成功之后,回调方法:
@Override public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
this.initialize();
this.loadUserList();
}
在UserListFragment类中,使用Inject注解,声明了一个成员@Inject UserListPresenter userListPresenter;
然后,调用UserComponent实例的方法inject,就可以将依赖userListPresenter注入到UserListFragment实例中。
调用userListPresenter的方法setView,建立Presenter和View之间的关系。
private void initialize() {
this.getComponent(UserComponent.class).inject(this);
this.userListPresenter.setView(this);
}
调用loadUserList方法,会加载所有用户数据。
/**
* Loads all users.
*/
private void loadUserList() {
this.userListPresenter.initialize();
}
界面的数据加载操作,都被封装在userListPresenter的initalize方法中。展示界面加载效果;执行获取数据操作;数据加载成功之后,显示对应的界面。这些都由userListPresenter来实现和控制。
 
5.UserListPresenter
 
A.
调用构造方法,构造方法的参数,由Dagger注入。
@Inject
public UserListPresenter(@Named("userList") UseCase getUserListUserCase, UserModelDataMapper userModelDataMapper) {
this.getUserListUseCase = getUserListUserCase;
this.userModelDataMapper = userModelDataMapper;
}
 
B.
提供initialize方法,供其它类调用。调用该方法,展示界面加载效果;获取所有用户数据。
/**
* 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层提供用例类。一个用例类表示一个表示一个业务,在这里表示获取用户列表。 }
6.UserListPresenter和UseCase的协作
 
 
UserListPresenter和UseCase的协作,使用
 
The RxJava Android Module 框架
 
 
1.UserListPresenter属于Presention层,它依赖domain层和data层。
 
2.UseCase是domain层,它的实例是GetUserListUseCase,这个是由Presention层中的依赖定义包指定的,如下:
com.fernandocejas.android10.sample.presentation.internal.di.modules.UserModule:
 
  @Provides @PerActivity @Named("userList") UseCase provideGetUserListUseCase(
      GetUserListUseCase getUserListUseCase) {
    return getUserListUseCase;
  }
 
-----
所以,GetUserListUseCase是必须是可注入的,要在其构造函数中添加@Inject注解。
 
3.UserListPresenter和GetUserListUseCase,UserListPresenter是一个订阅者;这个订阅者订阅的流是CloudUserDataStore实例的方法getUseEntityList()产出来之后,经过映射的流,
该流是长这个样子的:
 
  @Override public Observable<List<User>> getUsers() {
    //we always get all users from the cloud
    final UserDataStore userDataStore = this.userDataStoreFactory.createCloudDataStore();
    return userDataStore.getUserEntityList().map(userListEntityMapper);
  }
 
 
总结牵涉到的类及所在的层:
 
presentation层:UserListPresenter。
domain层: GetUseerListUseCase,UseCase
data层:UserDataRepository,CloudUserDataStore,UserDataStoreFactory
 
presentation层依赖domain层和data层。
 
 
7.整个项目使用到的框架技术
 
1.Dagger依赖生成,定义依赖的生成规则。
2.RxAndroid 响应式编程范式的应用,用事件流的方式来处理异步事件。
3.okHttp,网络请求应用到该框架。
 
8.整个项目阅读下来,可以学习到的东西
 
 
1.使用依赖注入,一个实例包含其它实例,在获取其它实例的使用,使用依赖注入,不将其它实例的生成方式暴露出去。这样,使用者只关注使用,无需关注怎样实例化,其它实例实例化的过程对使用者隐藏掉。
 
2.分层,分模块,这个要针对功能的含义来划分,根据关注点来划分。
presentation层,我们只关注界面展示,我们在这一层里面,我们只需要关注界面展示方面的代码,我们只放界面展示代码在这一层上,还有依赖生成的代码。在这层,我们重点只关注界面代码,还有依赖生成的规则。
 
domain层,领域层,用例层,从这个模块中包的命名就可以知道,它表示抽象程度高的业务,与系统平台无关的业务。在这个应用中,有两个业务,一个是获取用户列表,一个是获取用户的详细信息。
在这一层,存放业务的实现代码。
data层,数据获取,数据存储的实现代码放在这一层。
 
层之间的依赖规则:
presention层------>domain层-------获取数据---->data层
 
参考资料:http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/

例子Architecting Android…The clean way?----代码分析的更多相关文章

  1. Architecting Android…The clean way?

    Architecting Android-The clean way? 原文链接:http://fernandocejas.com/2014/09/03/architecting-android-th ...

  2. Android 4.2启动代码分析(一)

    Android系统启动过程分析 Android系统的框架架构图如下(来自网上):   Linux内核启动之后----->就到Android的Init进程 ----->进而启动Android ...

  3. ironic-conductor与ipa交互clean部分代码分析

    clean的动作会在provide和delete阶段才会触发 从代码分析: 对节点执行的node provide/deleted/clean会先发送到ironicclient ironicclient ...

  4. android recovery 主系统代码分析

    阅读完上一篇文章: http://blog.csdn.net/andyhuabing/article/details/9226569 我们已经清楚了如何进入正常模式和Recovery模式已有深刻理解了 ...

  5. android adb 流程原理代码分析(一)

    由于要用到adb的知识,但是对adb啥也不了解,看了下android的代码,adb的源码在system/core/adb下面,然后网上搜下了资料,发现很多大神的源码分析,瞬间信心爆棚,把大神写的博客都 ...

  6. android recovery 主系统代码分析【转】

    本文转载自:http://blog.csdn.net/andyhuabing/article/details/9248713 阅读完上一篇文章: http://blog.csdn.net/andyhu ...

  7. Android 静态代码分析工具

    简评: 作者在文中提到的三个静态代码分析工具不是互相替代的关系,各有各的侧重点,如果有需要完全可以同时使用. 静态代码分析是指无需运行被测代码,仅通过分析或检查源程序的语法.结构.过程.接口等来检查程 ...

  8. Android代码分析工具lint学习

    1 lint简介 1.1 概述 lint是随Android SDK自带的一个静态代码分析工具.它用来对Android工程的源文件进行检查,找出在正确性.安全.性能.可使用性.可访问性及国际化等方面可能 ...

  9. Android艺术——Bitmap高效加载和缓存代码分析(2)

    Bitmap的加载与缓存代码分析: 图片的压缩 比如有一张1024*768像素的图像要被载入内存,然而最终你要用到的图片大小其实只有128*96,那么我们会浪费很大一部分内存,这显然是没有必要的,下面 ...

随机推荐

  1. android入门 — PopupWindow

    第一步:自定义xml文件 第二步:获取LayoutInflater对象 第三步:调用inflate()方法获取View对象 第四步:创建PopupWindow对象 第五步:调用PopupWindow的 ...

  2. python爬虫 赶集网

    #coding=utf-8import requestsfrom lxml import etreefrom sqlalchemy import create_enginefrom sqlalchem ...

  3. 【Python】python更新数据库脚本两种方法

    最近项目的两次版本迭代中,根据业务需求的变化,需要对数据库进行更新,两次分别使用了不同的方式进行更新. 第一种:使用python的MySQLdb模块利用原生的sql语句进行更新   1 import ...

  4. SpringBoot JDBC/AOP

    JDBC 工程结构: pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmln ...

  5. java read读取方法 之 指定读取大小,根据返回值判断是否读取所有的内容 -1表示读取完毕

    当读取的内容小于指定的长度时候 字符数组里面会出现一部分字符是上一次遗留下来的情况

  6. 【bzoj4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+背包dp

    题目描述 给出 $n$ 个括号序列,从中选出任意个并将它们按照任意顺序连接起来,求以这种方式得到匹配括号序列的最大长度. 输入 第一行包含一个正整数n(1<=n<=300),表示括号序列的 ...

  7. Luogu1731 NOI1999生日蛋糕(搜索)

    非常经典的剪枝题然而一直没有写.感觉自己连普及组水平都没有了. 1.半径和高枚举范围满足加上后总体积不超过n且剩下每层还能放. 2.半径从大到小枚举,因为体积正比于半径平方而面积正比于半径,大的半径更 ...

  8. html的body内标签之超链接

    一,先来个简单的练练手,target="_blank" 的作用是在新的tab中打开页面.href 是超链接的意思. <!DOCTYPE html> <html l ...

  9. [国家集训队]最长双回文串 manacher

    ---题面--- 题解: 首先有一个直观的想法,如果我们可以求出对于位置i的最长后缀回文串和最长前缀回文串,那么我们枚举分界点然后合并前缀和后缀不就可以得到答案了么? 所以我们的目标就是求出这两个数列 ...

  10. [六省联考2017]分手是祝愿 期望DP

    表示每次看见期望的题就很懵逼... 但是这题感觉还是值得一做,有可借鉴之处 要是下面这段文字格式不一样的话(虽然好像的确不一样,我也不知道为什么,是直接从代码里面复制出来的,因为我一般都是习惯在代码里 ...