Skip to content

Commit 10ffb97

Browse files
committed
Add dependency-injection.md and Text format
1 parent 57a5791 commit 10ffb97

File tree

4 files changed

+71
-11
lines changed

4 files changed

+71
-11
lines changed

tech/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
####技术点列表
1111
技术点 | 作者
1212
:-- |:--
13-
[依赖注入](https://github.com/android-cn/blog/tree/master/java/dependency-injection) | [扔物线](https://github.com/rengwuxian)
14-
[注解](annotation.md) | [Trinea](https://github.com/Trinea)
13+
[依赖注入](https://github.com/android-cn/android-open-project-analysis/blob/master/tech/dependency-injection.md) | [扔物线](https://github.com/rengwuxian)
14+
[注解](https://github.com/android-cn/android-open-project-analysis/blob/master/tech/annotation.md) | [Trinea](https://github.com/Trinea)
1515
[动态代理](https://github.com/android-cn/android-open-project-analysis/blob/master/tech/proxy.md)| [Caij](https://github.com/Caij)
1616
事件传递 |
1717
[View 绘制流程](https://github.com/android-cn/android-open-project-analysis/blob/master/tech/viewdrawflow.md) | [lightSky](https://github.com/lightSky)

tech/annotation.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Java 注解 Annotation
22
----------------
3-
> 本文为 [Android 开源项目源码解析](https://github.com/android-cn/android-open-project-analysis) 公共技术点的 注解 部分
4-
> 分析者:[Trinea](https://github.com/trinea)
3+
> 本文为 [Android 开源项目源码解析](https://github.com/android-cn/android-open-project-analysis) 公共技术点中的 注解 部分
4+
分析者:[Trinea](https://github.com/Trinea),校对者:[Trinea](https://github.com/Trinea),校对状态:完成
5+
56

67
请先看进入 [Java Annotation 及几个常用开源项目注解原理简析](http://www.trinea.cn/android/java-annotation-android-open-source-analysis/) 查看,后面会在这里补充一份
78

tech/dependency-injection.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
依赖注入
2+
----------------
3+
> 本文为 [Android 开源项目源码解析](https://github.com/android-cn/android-open-project-analysis) 公共技术点中的 依赖注入 部分
4+
分析者:[扔物线](https://github.com/rengwuxian),校对者:[Trinea](https://github.com/Trinea),校对状态:完成
5+
6+
###1. 依赖
7+
如果在 Class A 中,有 Class B 的实例,则称 Class A 对 Class B 有一个依赖。例如下面类 Human 中用到一个 Father 对象,我们就说类 Human 对类 Father 有一个依赖。
8+
9+
```java
10+
public class Human {
11+
...
12+
Father father;
13+
...
14+
public Human() {
15+
father = new Father();
16+
}
17+
}
18+
```
19+
仔细看这段代码我们会发现存在一些问题:
20+
(1). 如果现在要改变 father 生成方式,如需要用`new Father(String name)`初始化 father,需要修改 Human 代码;
21+
(2). 如果想测试不同 Father 对象对 Human 的影响很困难,因为 father 的初始化被写死在了 Human 的构造函数中;
22+
(3). 如果`new Father()`过程非常缓慢,单测时我们希望用已经初始化好的 father 对象 Mock 掉这个过程也很困难。
23+
24+
###2. 依赖注入
25+
上面将依赖在构造函数中直接初始化是一种 Hard init 方式,弊端在于两个类不够独立,不方便测试。我们还有另外一种 Init 方式,如下:
26+
27+
```java
28+
public class Human {
29+
...
30+
Father father;
31+
...
32+
public Human(Father father) {
33+
this.father = father;
34+
}
35+
}
36+
```
37+
38+
上面代码中,我们将 father 对象作为构造函数的一个参数传入。在调用 Human 的构造方法之前外部就已经初始化好了 Father 对象。**像这种非自己主动初始化依赖,而通过外部来传入依赖的方式,我们就称为依赖注入。**
39+
现在我们发现上面 1 中存在的两个问题都很好解决了,简单的说依赖注入主要有两个好处:
40+
(1). 解耦,将依赖之间解耦。
41+
(2). 因为已经解耦,所以方便做单元测试,尤其是 Mock 测试。
42+
43+
###3. Java 中的依赖注入
44+
45+
依赖注入的实现有多种途径,而在 Java 中,使用注解是最常用的。通过在字段的声明前添加 @Inject 注解进行标记,来实现依赖对象的自动注入。
46+
47+
```java
48+
public class Human {
49+
...
50+
@Inject Father father;
51+
...
52+
public Human() {
53+
}
54+
}
55+
```
56+
57+
上面这段代码看起来很神奇:只是增加了一个注解,Father 对象就能自动注入了?这个注入过程是怎么完成的?
58+
59+
实质上,如果你只是写了一个 @Inject 注解,Father 并不会被自动注入。你还需要使用一个依赖注入框架,并进行简单的配置。现在 Java 语言中较流行的依赖注入框架有 [Google Guice](https://github.com/google/guice)[Spring](http://projects.spring.io/spring-framework/) 等,而在 Android 上比较流行的有 [RoboGuice](https://github.com/roboguice/roboguice)[Dagger](http://square.github.io/dagger/) 等。其中 Dagger 是我现在正在项目中使用的。如果感兴趣,你可以到 [Dagger 实现原理解析](https://github.com/android-cn/android-open-project-analysis/tree/master/dagger) 了解更多依赖注入和 Dagger 实现原理相关信息。

tech/minutiae.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
Android 开源项目源码解析细节点
22
====================================
33
> 本文为 [Android 开源项目源码解析](https://github.com/android-cn/android-open-project-analysis) 中 细节点 部分
4-
> 分析者:[Trinea](https://github.com/trinea),校对者:[Grumoon](https://github.com/grumoon)[Trinea](https://github.com/trinea),校对状态:未完成
4+
> 分析者:[Trinea](https://github.com/trinea),校对者:[Trinea](https://github.com/trinea),校对状态:未完成
55
6-
###Volley 细节点
7-
#####(1) HttpURLConnection 与 HttpClient
6+
###1. Volley 细节点
7+
####(1) HttpURLConnection 与 HttpClient
88

9-
###Android Universal Image Loader 细节点
10-
#####(1) FlushedInputStream.java
9+
###2. Android Universal Image Loader 细节点
10+
####(1) FlushedInputStream.java
1111
为了解决早期 Android 版本`BitmapFactory.decodeStream(…)`在慢网络情况下 decode image 异常的 Bug。
1212
主要通过重写`FilterInputStream`的 skip(long n) 函数解决,确保 skip(long n) 始终跳过了 n 个字节。如果返回结果即跳过的字节数小于 n,则不断循环直到 skip(long n) 跳过 n 字节或到达文件尾。
1313
见:http://code.google.com/p/android/issues/detail?id=6066
1414

15-
####(2). BaseImageDownloader.getStreamFromNetwork(String imageUri, Object extra)
15+
###(2). BaseImageDownloader.getStreamFromNetwork(String imageUri, Object extra)
1616
通过`HttpURLConnection`从网络获取图片的`InputStream`。支持 response code 为 3xx 的重定向。这里有个小细节代码如下:
1717
```java
1818
try {
@@ -26,7 +26,7 @@ try {
2626
在发生异常时会调用`conn.getErrorStream()`继续读取 Error Stream,这是为了利于网络连接回收及复用。但有意思的是在 Froyo(2.2) 之前,HttpURLConnection 有个重大 Bug,调用 close() 函数会影响连接池,导致连接复用失效,不少库通过在 2.3 之前使用 AndroidHttpClient 解决这个问题。
2727
见:http://docs.oracle.com/javase/6/docs/technotes/guides/net/http-keepalive.html
2828

29-
#####(3). ViewAware.ViewAware(View view, boolean checkActualViewSize)
29+
####(3). ViewAware.ViewAware(View view, boolean checkActualViewSize)
3030
构造函数。
3131
`view`表示需要显示图片的对象。
3232
`checkActualViewSize`表示通过`getWidth()``getHeight()`获取图片宽高时返回真实的宽和高,还是`LayoutParams`的宽高,true 表示返回真实宽和高。

0 commit comments

Comments
 (0)