Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 69be2a2

Browse files
author
deeper
committed
安全的组合模式
1 parent 927fa7e commit 69be2a2

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

‎src/com/ruoxu/pattern/composite/Demo.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
* 总结:
55
* 组合模式比较简单,用的也不多
66
* 组合模式分为两种: 1.安全的组合模式 2.不安全的组合模式(透明的组合模式)
7+
*
8+
* 本模式用了3个例子,1.安全的组合模式 2.不安全的组合模式 3.文件夹和文件案例
9+
* 建议使用安全的组合模式,安卓View和ViewGroup即为【安全的组合模式】
710
*/
811
public class Demo {
912
public static void main(String[] args) {
10-
13+
// nothing to do
1114
}
1215
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ruoxu.pattern.composite.safe;
2+
/**
3+
* 抽象根节点
4+
*/
5+
public abstract class Component {
6+
protected String name; // 节点名
7+
8+
public Component(String name) {
9+
this.name = name;
10+
}
11+
12+
public abstract void doSomething();
13+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.ruoxu.pattern.composite.safe;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 具体枝干节点(相当于文件夹)
8+
*/
9+
public class Composite extends Component{
10+
private List<Component> components = new ArrayList<>();
11+
12+
public Composite(String name) {
13+
super(name);
14+
}
15+
16+
@Override
17+
public void doSomething() {
18+
System.out.println(name);
19+
if(null != components){
20+
for(Component c: components){
21+
c.doSomething();
22+
}
23+
}
24+
}
25+
26+
/**
27+
* 添加子节点
28+
*/
29+
public void addChild(Component child){
30+
components.add(child);
31+
}
32+
33+
/**
34+
* 移除子节点
35+
*/
36+
public void removeChild(Component child){
37+
components.remove(child);
38+
}
39+
40+
/**
41+
* 获取子节点
42+
*/
43+
public Component getChild(int index){
44+
return components.get(index);
45+
}
46+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.ruoxu.pattern.composite.safe;
2+
3+
public class Demo {
4+
public static void main(String[] args) {
5+
Composite root = new Composite("root");
6+
7+
Composite branch1 = new Composite("branch1");
8+
Composite branch2 = new Composite("branch2");
9+
10+
Leaf leaf1 = new Leaf("leaf1");
11+
Leaf leaf2 = new Leaf("leaf2");
12+
13+
branch1.addChild(leaf1);
14+
branch2.addChild(leaf2);
15+
16+
root.addChild(branch1);
17+
root.addChild(branch2);
18+
19+
root.doSomething();
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ruoxu.pattern.composite.safe;
2+
/**
3+
* 叶子结点
4+
*/
5+
public class Leaf extends Component{
6+
7+
public Leaf(String name) {
8+
super(name);
9+
}
10+
11+
@Override
12+
public void doSomething() {
13+
System.out.println(name);
14+
}
15+
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /