【图解设计模式】Composite模式

能够使容器与内容具有一致性,创造出递归结构。

示例

列出文件和文件夹的一览。

类图

eojzoF.png

Entry类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public abstract class Entry {
public abstract String getName();
public abstract int getSize();

public Entry add(Entry entry) throws FileTreatmentException {
throw new FileTreatmentException();
}

public void printList() {
printList("");
}

protected abstract void printList(String prefix);

@Override
public String toString() {
return getName() + " (" + getSize() + ")";
}
}

File类

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 name;
}

@Override
public int getSize() {
return size;
}

@Override
protected void printList(String prefix) {
System.out.println(prefix + "/" + this);
}
}

Directory类

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
41
42
43
44
45
46
import java.util.ArrayList;
import java.util.Iterator;

public class Directory extends Entry {
private String name;
private ArrayList directory = new ArrayList();

public Directory(String name) {
this.name = name;
}

@Override
public String getName() {
return name;
}

@Override
public int getSize() {
int size = 0;

Iterator it = directory.iterator();
while (it.hasNext()) {
Entry entry = (Entry) it.next();
size += entry.getSize();
}

return size;
}

@Override
protected void printList(String prefix) {
System.out.println(prefix + "/" + this);

Iterator it = directory.iterator();
while (it.hasNext()) {
Entry entry = (Entry) it.next();
entry.printList(prefix + "/" + name);
}
}

@Override
public Entry add(Entry entry) {
directory.add(entry);
return this;
}
}

FileTreatmentException类

1
2
3
4
5
6
7
public class FileTreatmentException extends RuntimeException {
public FileTreatmentException() {}

public FileTreatmentException(String msg) {
super(msg);
}
}

Main类

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
public class Main {

public static void main(String[] args) {
try {
System.out.println("Making root entries...");
Directory rootdir = new Directory("root");
Directory bindir = new Directory("bin");
Directory tmpdir = new Directory("tmp");
Directory usrdir = new Directory("usr");
rootdir.add(bindir);
rootdir.add(tmpdir);
rootdir.add(usrdir);
bindir.add(new File("vi", 10000));
bindir.add(new File("latex", 20000));
rootdir.printList();

System.out.println("");

System.out.println("Making user entries...");
Directory yuki = new Directory("yuki");
Directory hanako = new Directory("hanako");
Directory tomura = new Directory("tomura");
usrdir.add(yuki);
usrdir.add(hanako);
usrdir.add(tomura);
yuki.add(new File("diary.html", 100));
yuki.add(new File("Composite.java", 200));
hanako.add(new File("memo.tex", 300));
tomura.add(new File("game.doc", 400));
tomura.add(new File("junk.mail", 500));
rootdir.printList();
} catch (FileTreatmentException e) {
e.printStackTrace();
}
}
}

运行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Making root entries...
/root (30000)
/root/bin (30000)
/root/bin/vi (10000)
/root/bin/latex (20000)
/root/tmp (0)
/root/usr (0)

Making user entries...
/root (31500)
/root/bin (30000)
/root/bin/vi (10000)
/root/bin/latex (20000)
/root/tmp (0)
/root/usr (1500)
/root/usr/yuki (300)
/root/usr/yuki/diary.html (100)
/root/usr/yuki/Composite.java (200)
/root/usr/hanako (300)
/root/usr/hanako/memo.tex (300)
/root/usr/tomura (900)
/root/usr/tomura/game.doc (400)
/root/usr/tomura/junk.mail (500)

登场角色

Leaf(树叶)

表示“内容”的角色。在该角色中不能放入其他对象。在示例程序中,由File类扮演此角色。

Composite(复合物)

表示容器的角色。可以在其中放入Leaf角色和Composite角色。在示例程序中,由Directory类扮演此角色。

Component

使Leaf角色和Composite角色具有一致性的角色。Component角色是Leaf角色和Composite角色的父类。在示例程序中,由Entry类扮演此角色。

Client

使用Composite模式的角色。在示例程序中,由Main类扮演此角色。

类图

eTqKIJ.png