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 dbba1d1

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

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
public class Demo {
44
public static void main(String[] args) {
5+
// 从下面的例子里面很明显可以看出来 :两个类虽然都实现了共同的抽象,却都是以实现类来new的,这与【依赖倒置】相违背,定义的抽象Component起的作用就不大。
6+
// 为了防止这个问题,不安全的组合模式出现了(虽然不安全,但正确使用也没有多大问题)
57
Composite root = new Composite("root");
68

79
Composite branch1 = new Composite("branch1");
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.ruoxu.pattern.composite.unsafe;
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+
14+
public abstract void addChild(Component child);
15+
16+
public abstract void removeChild(Component child);
17+
18+
public abstract Component getChild(int index);
19+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.ruoxu.pattern.composite.unsafe;
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+
@Override
30+
public void addChild(Component child){
31+
components.add(child);
32+
}
33+
34+
/**
35+
* 移除子节点
36+
*/
37+
@Override
38+
public void removeChild(Component child){
39+
components.remove(child);
40+
}
41+
42+
/**
43+
* 获取子节点
44+
*/
45+
@Override
46+
public Component getChild(int index){
47+
return components.get(index);
48+
}
49+
50+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.ruoxu.pattern.composite.unsafe;
2+
3+
public class Demo {
4+
public static void main(String[] args) {
5+
Component root = new Composite("root");
6+
7+
Component branch1 = new Composite("branch1");
8+
Component branch2 = new Composite("branch2");
9+
10+
Component leaf1 = new Leaf("leaf1");
11+
Component 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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.ruoxu.pattern.composite.unsafe;
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+
@Override
17+
public void addChild(Component child) {
18+
throw new UnsupportedOperationException("叶子节点没有子节点");
19+
}
20+
21+
@Override
22+
public void removeChild(Component child) {
23+
throw new UnsupportedOperationException("叶子节点没有子节点");
24+
}
25+
26+
@Override
27+
public Component getChild(int index) {
28+
throw new UnsupportedOperationException("叶子节点没有子节点");
29+
}
30+
31+
}

0 commit comments

Comments
(0)

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