Skip to content

Commit 3c06733

Browse files
author
Liu Yuning
committed
add composite DP
1 parent d4deb16 commit 3c06733

4 files changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package designpattern.composite;
2+
3+
/**
4+
* Component为组合中的对象声明接口,在适当情况下,实现所有类共有接口的默认行为。
5+
*
6+
* @author liu yuning
7+
*
8+
*/
9+
public abstract class Component {
10+
protected String name;
11+
12+
public Component(String name) {
13+
this.name = name;
14+
}
15+
16+
public abstract void add(Component component);
17+
18+
public abstract void remove(Component component);
19+
20+
public abstract void display(int depth);
21+
22+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package designpattern.composite;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import util.StringUtil;
7+
8+
/**
9+
* 定义有枝节点行为,用来存储子部件
10+
*
11+
* @author liu yuning
12+
*
13+
*/
14+
public class Composite extends Component {
15+
private List<Component> children = new ArrayList<Component>();
16+
17+
public Composite(String name) {
18+
super(name);
19+
}
20+
21+
@Override
22+
public void add(Component component) {
23+
children.add(component);
24+
}
25+
26+
@Override
27+
public void remove(Component component) {
28+
children.remove(component);
29+
}
30+
31+
@Override
32+
public void display(int depth) {
33+
// 显示其枝节点名称,并对其下级进行遍历
34+
System.out.println(StringUtil.repeatableString("-", depth) + this.name);
35+
36+
for (Component component : children) {
37+
component.display(depth + 2);
38+
}
39+
40+
}
41+
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package designpattern.composite;
2+
3+
/**
4+
* 客户端。通过Component接口操作组合部件的对象
5+
*
6+
* @author liu yuning
7+
*
8+
*/
9+
public class CompositeClient {
10+
11+
public static void main(String[] args) {
12+
// 生成树根,根上长出两叶Leaf A和Leaf B
13+
Composite root = new Composite("root");
14+
root.add(new Leaf("Leaf A"));
15+
root.add(new Leaf("Leaf B"));
16+
17+
// 根上长出分支Composite X,分支上也有两叶Leaf X-A和Leaf X-B
18+
Composite compositeX = new Composite("Composite X");
19+
compositeX.add(new Leaf("Leaf X-A"));
20+
compositeX.add(new Leaf("Leaf X-B"));
21+
root.add(compositeX);
22+
23+
// 在Composite X上再长出分支Composite X-Y,分支上也有两叶Leaf X-Y-A和Leaf X-Y-B
24+
Composite compositeXY = new Composite("Composite X-Y");
25+
compositeXY.add(new Leaf("Leaf X-Y-A"));
26+
compositeXY.add(new Leaf("Leaf X-Y-B"));
27+
compositeX.add(compositeXY);
28+
29+
// 显示大树的样子
30+
root.display(1);
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package designpattern.composite;
2+
3+
import util.StringUtil;
4+
5+
/**
6+
* Leaf在组合中表示叶节点对象,叶节点没有子节点
7+
*
8+
* @author liu yuning
9+
*
10+
*/
11+
public class Leaf extends Component {
12+
13+
public Leaf(String name) {
14+
super(name);
15+
}
16+
17+
@Override
18+
public void add(Component component) {
19+
System.out.println("cannot add to a leaf");
20+
}
21+
22+
@Override
23+
public void remove(Component component) {
24+
System.out.println("cannot remove from a leaf");
25+
}
26+
27+
@Override
28+
public void display(int depth) {
29+
// 通过“-”的数目显示级别
30+
System.out.println(StringUtil.repeatableString("-", depth) + this.name);
31+
}
32+
33+
}

0 commit comments

Comments
 (0)