Skip to content

Commit 46709c0

Browse files
committed
Signed-off-by: tiny-times <[email protected]>
1 parent c2da642 commit 46709c0

File tree

1 file changed

+158
-52
lines changed

1 file changed

+158
-52
lines changed

composite/tiny-times/readme.md

+158-52
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,164 @@
1-
Android设计模式源码解析之${模式名}
2-
====================================
3-
> 本文为 [Android 设计模式源码解析](https://github.com/simple-android-framework-exchange/android_design_patterns_analysis) 中 ${模式名} 分析
4-
> Android系统版本: ${系统版本号,例如 4.2.1}
5-
> 分析者:[${分析者}](${分析者 Github 地址}),分析状态:未完成,校对者:[Mr.Simple](https://github.com/bboyfeiyu),校对状态:未开始
1+
本文为 Android设计模式源码中 组合模式 分析
2+
Android系统版本: 5.0
3+
分析者:Tiny-Times,分析状态:未完成,校对者:无,校对状态:未开始
64

5+
## 1\. 模式介绍
76

8-
`复制一份到自己的项目文件夹下,然后根据自己项目替换掉 ${} 内容,删掉本行及上面两行。`
9-
10-
该任务不仅要用java写出该模式的一个简单示例,还有分析该模式在Android源码中的应用,可以参考[Mr.Simple的单例模式](singleton/mr.simple/readme.md)[Mr.Simple的观察者模式](observer/mr.simple/readme.md)
11-
12-
13-
## 1. 模式介绍
14-
15-
### 模式的定义
16-
`模式的一句话定义`
7+
### 模式的定义
178

9+
        组合模式(Composite Pattern)又叫作部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。 GoF在《设计模式》一书中这样定义组合模式:将对象组合成树形结构以表示“部分-整体”的层次结构。使得用户对单个对象和组合对象的使用具有一致性。
1810

1911
### 模式的使用场景
20-
21-
22-
23-
## 2. UML类图
24-
`这里是该设计模式的经典UML图`
25-
26-
### 角色介绍
27-
`对UML图中的各个角色进行介绍`
28-
29-
30-
31-
32-
## 3. 模式的简单实现
33-
### 简单实现的介绍
34-
`自己实现一个小型模式案例,通过这个案例让读者了解这个模式的一般应用`
35-
36-
### 实现源码
37-
`上述案例的源码实现`
38-
39-
40-
### 总结
41-
`对上述的简单示例进行总结说明`
42-
43-
44-
45-
46-
## Android源码中的模式实现
47-
`分析源码中的模式实现,列出相关源码,以及使用该模式原因等`
48-
49-
50-
51-
52-
## 4. 杂谈
53-
该模式的优缺点以及自己的一些感悟,非所有项目必须。
54-
55-
56-
57-
`写完相关内容之后到开发群告知管理员,管理员安排相关人员进行审核,审核通过之后即可。`
5812

13+
* 表示对象的部分-整体层次结构。
14+
* 从一个整体中能够独立出部分模块或功能的场景。
15+
16+
## 2\. UML类图
17+
18+
* * *
19+
20+
![组合模式通用类图][1]
21+
22+
### 角色分析
23+
24+
* Component抽象构件角色 :定义参加组合对象的共有方法和属性,可以定义一些默认的行为或属性。
25+
* Leaf叶子构件 : 叶子对象,其下再也没有其他的分支。
26+
* Composite树枝构件 :树枝对象,它的作用是组合树枝节点和叶子节点形成一个树形结构。
27+
28+
## 3\. 该模式的实现实例
29+
30+
* * *
31+
32+
抽象构件 Component.java:
33+
34+
public abstract class Component {
35+
//个体和整体都具有的共享
36+
public void doSomething(){
37+
//业务逻辑
38+
}
39+
}
40+
41+
42+
树枝构件 Composite.java
43+
44+
public class Composite extends Component {
45+
//构件容器
46+
private ArrayList<Component> componentArrayList = new ArrayList<Component>();
47+
//增加一个叶子构件或树枝构件
48+
public void add(Component component){
49+
this.componentArrayList.add(component);
50+
}
51+
//删除一个叶子构件或树枝构件
52+
public void remove(Component component){
53+
this.componentArrayList.remove(component);
54+
}
55+
//获得分支下的所有叶子构件和树枝构件
56+
public ArrayList<Component> getChildren(){
57+
return this.componentArrayList;
58+
}
59+
}
60+
61+
62+
树叶构件 Leaf.java
63+
64+
public class Leaf extends Component {
65+
66+
//可以覆写父类方法
67+
public void doSomething(){
68+
}
69+
70+
}
71+
72+
73+
场景类 Client.java
74+
75+
public class Client {
76+
public static void main(String[] args) {
77+
//创建一个根节点
78+
Composite root = new Composite();
79+
root.doSomething();
80+
//创建一个树枝构件
81+
Composite branch = new Composite();
82+
//创建一个叶子节点
83+
Leaf leaf = new Leaf();
84+
//建立整体
85+
root.add(branch);
86+
branch.add(leaf);
87+
}
88+
//通过递归遍历树
89+
public static void display(Composite root){
90+
for(Component c:root.getChildren()){
91+
if(c instanceof Leaf){ //叶子节点
92+
c.doSomething();
93+
}else{ //树枝节点
94+
display((Composite)c);
95+
}
96+
}
97+
}
98+
}
99+
100+
101+
### 组合模式在Android源码中的应用
102+
103+
* * *
104+
105+
**Adnroid系统中采用组合模式的组合视图类图:**
106+
107+
![enter image description here][2]
108+
109+
**具体实现代码**
110+
111+
View.java
112+
113+
public class View ....{
114+
//此处省略无关代码...
115+
}
116+
117+
118+
ViewGroup.java
119+
120+
public abstract class ViewGroup extends View ...{
121+
122+
//增加子节点
123+
public void addView(View child, int index) {
124+
125+
}
126+
//删除子节点
127+
public void removeView(View view) {
128+
129+
}
130+
//查找子节点
131+
public View getChildAt(int index) {
132+
try {
133+
return mChildren[index];
134+
} catch (IndexOutOfBoundsException ex) {
135+
return null;
136+
}
137+
}
138+
}
139+
140+
141+
## 4\. 注意事项
142+
143+
* * *
144+
145+
使用组合模式组织起来的对象具有出色的层次结构,每当对顶层组合对象执行一个操作的时候,实际上是在对整个结构进行深度优先的节点搜索。但是这些优点都是用操作的代价换取的,比如顶级每执行一次 store.show 实际的操作就是整一颗树形结构的节点均遍历执行一次。
146+
147+
## 5\. 杂谈
148+
149+
* * *
150+
151+
**优点**
152+
153+
* 不破坏封装,整体类与局部类之间松耦合,彼此相对独立 。
154+
* 具有较好的可扩展性。
155+
* 支持动态组合。在运行时,整体对象可以选择不同类型的局部对象。
156+
* 整体类可以对局部类进行包装,封装局部类的接口,提供新的接口。
157+
158+
**缺点**
159+
160+
* 整体类不能自动获得和局部类同样的接口。
161+
* 创建整体类的对象时,需要创建所有局部类的对象 。
162+
163+
[1]: http://belial.me/wp-content/uploads/2015/03/QQ截图20150318225518.png
164+
[2]: http://belial.me/wp-content/uploads/2015/03/QQ截图20150315115212.png

0 commit comments

Comments
 (0)