Luckylau's Blog

设计模式之合成模式

组合模式(Composite Pattern),又叫部分整体模式,依据树形结构来组合对象,是用来表示部分以及整体层次的一种递归式结构的模式。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。

以文件系统目录结构为例

无论是目录还是文件,在Linux中我们都认为是广义的“文件”,为了方便阐述,我们把这个广义的“文件”叫做Entry吧,显然目录DirectoryFile都是一种Entry

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public abstract class Entry {
public abstract String getName();
public abstract int getSize();
public abstract Entry add(Entry entry) {
throw new RuntimeException();
}
public void printList() {
printList("");
}
public abstract void printList(String prefix);
@Override
public String toString() {
return getName() + "(" + getSize() + ")";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class File extends Entry {
private String name;
private int size;
public File(String name, int size) {
this.name = name;
this.size = size;
}
@Override
public String getName() {
return this.name;
}
@Override
public int getSize() {
return this.size;
}
@Override
public void printList(String prefix) {
System.out.println(prefix + "/" + this);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Directory extends Entry {
private String name;
private List<Entry> items = new ArrayList<Entry>();
public Directory(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public int getSize() {
int size = 0;
Iterator it = this.items.iterator();
while (it.hasNext()) {
Entry entry = (Entry) it.next();
size += entry.getSize();
}
return size;
}
@Override
public Entry add(Entry entry) {
items.add(entry);
return this;
}
@Override
public void printList(String prefix) {
System.out.println(prefix + "/" + getName());
Iterator it = items.iterator();
while (it.hasNext()) {
Entry entry = (Entry) it.next();
entry.printList(prefix + "/" + this.name);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Client {
public static void main(String[] args) {
Entry bindir = new Directory("bin");
Entry usrdir = new Directory("usr");
Entry tmpdir = new Directory("lib");
bindir
.add(new File("bash", 4))
.add(new File("ls", 6))
.add(new File("ip", 8));
usrdir
.add(new Directory("bin")
.add(new File("top", 10))
.add(new File("ssh", 12)))
.add(new Directory("local")
.add(new Directory("bin")
.add(new File("eclipse", 4))
.add(new File("idea", 4)))
.add(new Directory("src")));
tmpdir
.add(new File("test.txt", 12));
Entry rootdir = new Directory("root");
rootdir.add(bindir).add(usrdir).add(tmpdir);
rootdir.printList();
}
}
Luckylau wechat
如果对您有价值,看官可以打赏的!