Skip to content

Commit 736e964

Browse files
committed
add Visitor example
1 parent 3448df5 commit 736e964

File tree

8 files changed

+323
-0
lines changed

8 files changed

+323
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ I'm adding new samples from time to time.
99
5. Singleton (EagerInitialization,as Enum,LazyInitialization,StaticBlockInitialization,ThreadSafe)
1010
6. State
1111
7. Strategy
12+
8. Visitor
1213

1314
# Contribute
1415

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015 Massimo Caliman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
package io.github.mcaliman.patterns.visitor;
25+
26+
import java.io.File;
27+
28+
/**
29+
* @author Massimo Caliman
30+
*/
31+
public class Document implements Node {
32+
33+
private final File file;
34+
35+
public Document(File file) {
36+
this.file = file;
37+
}
38+
39+
public String getName() {
40+
return file.getName();
41+
}
42+
43+
@Override
44+
public void accept(Visitor v) {
45+
v.visit(this);
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015 Massimo Caliman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
package io.github.mcaliman.patterns.visitor;
25+
26+
import java.io.File;
27+
28+
/**
29+
* @author Massimo Caliman
30+
*/
31+
public class Folder implements Node {
32+
33+
private final File directory;
34+
35+
public Folder(File directory) {
36+
this.directory = directory;
37+
}
38+
39+
@Override
40+
public void accept(Visitor v) {
41+
v.visit(this);
42+
}
43+
44+
public String getName() {
45+
return directory.getName();
46+
}
47+
48+
public Node[] getChildren() {
49+
File[] files = directory.listFiles();
50+
Node[] nodes = new Node[files.length];
51+
for (int i = 0; i < files.length; i++) {
52+
nodes[i] = files[i].isDirectory() ? new Folder(files[i]) : new Document(files[i]);
53+
54+
}
55+
return nodes;
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015 Massimo Caliman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
package io.github.mcaliman.patterns.visitor;
25+
26+
/**
27+
* @author Massimo Caliman
28+
*/
29+
public interface Node {
30+
31+
void accept(Visitor v);
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015 Massimo Caliman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
package io.github.mcaliman.patterns.visitor;
25+
26+
/**
27+
* @author Massimo Caliman
28+
*/
29+
public class NullVisitor implements Visitor {
30+
31+
@Override
32+
public void visit(Document file) {
33+
//do nothing
34+
}
35+
36+
@Override
37+
public void visit(Folder directory) {
38+
Node[] children = directory.getChildren();
39+
for (Node file : children) {
40+
file.accept(this);
41+
}
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015 Massimo Caliman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
package io.github.mcaliman.patterns.visitor;
25+
26+
import static java.lang.System.out;
27+
28+
/**
29+
* @author Massimo Caliman
30+
*/
31+
public class PrintVisitor implements Visitor {
32+
33+
private int level = 0;//livello di profondità per l'indentazione
34+
35+
@Override
36+
public void visit(Document document) {
37+
for (int i = 0; i < level; i++) {
38+
out.print(" ");
39+
}
40+
out.println("Document: " + document.getName());
41+
}
42+
43+
@Override
44+
public void visit(Folder folder) {
45+
for (int i = 0; i < level; i++) {
46+
out.print(" ");
47+
}
48+
out.println("Folder: " + folder.getName());
49+
level++;
50+
Node[] children = folder.getChildren();
51+
for (Node node : children) {
52+
node.accept(this);
53+
}
54+
level--;
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015 Massimo Caliman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
package io.github.mcaliman.patterns.visitor;
25+
26+
/**
27+
* @author Massimo Caliman
28+
*/
29+
public interface Visitor {
30+
31+
void visit(Document node);
32+
33+
void visit(Folder node);
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015 Massimo Caliman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
package io.github.mcaliman.patterns.visitor;
25+
26+
import java.io.File;
27+
import java.util.logging.Level;
28+
import java.util.logging.Logger;
29+
30+
/**
31+
* @author Massimo Caliman
32+
*/
33+
public class VisitorPatternJavaExamples {
34+
35+
private static final Logger LOG = Logger.getLogger(VisitorPatternJavaExamples.class.getName());
36+
37+
public static void main(String[] args) {
38+
Folder node = new Folder(new File("/home/mcaliman/Dropbox"));
39+
/*LOG.log(Level.INFO,"test PrintVisitor");
40+
LOG.log(Level.INFO,"=================");
41+
*/
42+
node.accept(new PrintVisitor());
43+
LOG.log(Level.INFO, "test NullVisitor");
44+
LOG.log(Level.INFO, "=================");
45+
node.accept(new NullVisitor());
46+
}
47+
48+
}

0 commit comments

Comments
 (0)