forked from echoTheLiar/JavaCodeAcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisitorClient.java
More file actions
39 lines (33 loc) · 939 Bytes
/
VisitorClient.java
File metadata and controls
39 lines (33 loc) · 939 Bytes
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
package designpattern.visitor;
import java.util.ArrayList;
import java.util.stream.Collectors;
/**
* 客户端
*
* @author liu yuning
*
*/
public class VisitorClient {
public static void main(String[] args) {
ObjectStructure o = new ObjectStructure();
o.attach(new ConcreteElementA());
o.attach(new ConcreteElementB());
ConcreteVisitor1 visitor1 = new ConcreteVisitor1();
ConcreteVisitor2 visitor2 = new ConcreteVisitor2();
//在不改变元素的结构前提下,由访问者去实现对各元素的不同操作,
o.accept(visitor1);
o.accept(visitor2);
TestAc testAc1 = new TestAc();
TestAc testAc2 = new TestAc();
ArrayList<TestAc> objects = new ArrayList<>();
objects.add(testAc1);
objects.add(testAc2);
objects.stream().map(object->{
object.setName("sf");
return object;
}).collect(Collectors.toList());
for (TestAc object : objects) {
System.out.println(object.getName());
}
}
}