设计模式-访问者模式

访问者模式

定义

在访问者模式(Visitor Pattern)中,我们使用了一个访问者类,它改变了元素类的执行算法。通过这种方式,元素的执行算法可以随着访问者改变而改变。根据模式,元素对象已接受访问者对象,这样访问者对象就可以处理元素对象上的操作。

类图

访问者模式类图

Visitor

负责对数据结构中每个具体的元素声明一个用于访问XXX的visit(XXX)方法。visit(XXX)是用于处理XXX的方法,负责实现该方法的是ConcreteVisitor。

ConcreteVisitor

负责实现Visitor所定义的接口。它要实现所有的visit(XXX)方法,即实现如何处理每个ConcreteElement

Element(元素)

表示Visitor角色的访问对象。它声明了接受访问者的accept方法。accept方法接收到的参数是Visitor。

ObjectStructure

负责处理Element的集合。ConcreteVisitor为每个Element都准备了处理方法。

示例

类图

装饰器模式示例类图

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
public class Directory extends Entry {
private String name;
private ArrayList<Entry> dir = new ArrayList<>();
public Directory(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getSize() {
int size = 0;
Iterator<Entry> it = dir.iterator();
while (it.hasNext()) {
Entry entry = (Entry)it.next();
size += entry.getSize();
}
return size;
}
public Entry add(Entry entry) {
dir.add(entry);
return this;
}
public Iterator<Entry> iterator() {
return dir.iterator();
}
public void accept(Visitor v) {
v.visit(this);
}
}

Element

1
2
3
4
public interface Element {
public abstract void accept(Visitor v);
}

Entry

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public abstract class Entry implements Element {
public abstract String getName();
public abstract int getSize();
public Entry add(Entry entry) throws FileTreatmentException {
throw new FileTreatmentException();
}
public Iterator<Entry> iterator() throws FileTreatmentException {
throw new FileTreatmentException();
}
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
public class File extends Entry {
private String name;
private int size;
public File(String name, int size) {
this.name = name;
this.size = size;
}
public String getName() {
return name;
}
public int getSize() {
return size;
}
public void accept(Visitor v) {
v.visit(this);
}
}

FileTreatmentException

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

ListVisitor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class ListVisitor extends Visitor {
private String currentdir = "";
public void visit(File file) {
System.out.println(currentdir + "/" + file);
}
public void visit(Directory directory) {
System.out.println(currentdir + "/" + directory);
String savedir = currentdir;
currentdir = currentdir + "/" + directory.getName();
Iterator<Entry> it = directory.iterator();
while (it.hasNext()) {
Entry entry = (Entry)it.next();
entry.accept(this);
}
currentdir = savedir;
}
}

Visitor

1
2
3
4
public abstract class Visitor {
public abstract void visit(File file);
public abstract void visit(Directory directory);
}

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
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.accept(new ListVisitor());

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.accept(new ListVisitor());
} catch (FileTreatmentException e) {
e.printStackTrace();
}
}
}

总结

Visitor模式的目的是将处理从数据结构中分离出来

0%