Skip to content

Commit 9c26833

Browse files
dszczukockipivovarit
authored andcommitted
BAEL-1792 overview of visitor design pattern (eugenp#4352)
1 parent c99aae6 commit 9c26833

File tree

7 files changed

+101
-0
lines changed

7 files changed

+101
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.visitor;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Document extends Element {
7+
8+
List<Element> elements = new ArrayList<>();
9+
10+
public Document(String uuid) {
11+
super(uuid);
12+
}
13+
14+
@Override
15+
public void accept(Visitor v) {
16+
for (Element e : this.elements) {
17+
e.accept(v);
18+
}
19+
}
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.visitor;
2+
3+
public abstract class Element {
4+
5+
public String uuid;
6+
7+
public Element(String uuid) {
8+
this.uuid = uuid;
9+
}
10+
11+
public abstract void accept(Visitor v);
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.visitor;
2+
3+
public class ElementVisitor implements Visitor {
4+
5+
@Override
6+
public void visit(XmlElement xe) {
7+
System.out.println("processing xml element with uuid: " + xe.uuid);
8+
}
9+
10+
@Override
11+
public void visit(JsonElement je) {
12+
System.out.println("processing json element with uuid: " + je.uuid);
13+
}
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.visitor;
2+
3+
public class JsonElement extends Element {
4+
5+
public JsonElement(String uuid) {
6+
super(uuid);
7+
}
8+
9+
public void accept(Visitor v) {
10+
v.visit(this);
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.visitor;
2+
3+
public interface Visitor {
4+
5+
void visit(XmlElement xe);
6+
7+
void visit(JsonElement je);
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.visitor;
2+
3+
import java.util.UUID;
4+
5+
public class VisitorDemo {
6+
7+
public static void main(String[] args) {
8+
9+
Visitor v = new ElementVisitor();
10+
11+
Document d = new Document(generateUuid());
12+
d.elements.add(new JsonElement(generateUuid()));
13+
d.elements.add(new JsonElement(generateUuid()));
14+
d.elements.add(new XmlElement(generateUuid()));
15+
16+
d.accept(v);
17+
}
18+
19+
private static String generateUuid() {
20+
return UUID.randomUUID()
21+
.toString();
22+
}
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.visitor;
2+
3+
public class XmlElement extends Element {
4+
5+
public XmlElement(String uuid) {
6+
super(uuid);
7+
}
8+
9+
public void accept(Visitor v) {
10+
v.visit(this);
11+
}
12+
}

0 commit comments

Comments
 (0)