728x90
반응형
01.방문자패턴 적용 사례
01.1 자바
- FileVisitor, SimpleFileVisitor
- AnnotationValueVisitor
- ElementVisitor
package me.whiteship.designpatterns._03_behavioral_patterns._23_visitor._03_java;
import me.whiteship.designpatterns._03_behavioral_patterns._23_visitor._02_after.Triangle;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
public class SearchFileVisitor implements FileVisitor<Path> {
private String fileToSearch;
private Path startingDirectory;
public SearchFileVisitor(String fileToSearch, Path startingDirectory) {
this.fileToSearch = fileToSearch;
this.startingDirectory = startingDirectory;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (fileToSearch.equals(file.getFileName().toString())) {
System.out.println("found " + file.getFileName());
return FileVisitResult.TERMINATE;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
exc.printStackTrace(System.out);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (Files.isSameFile(startingDirectory, dir)) {
System.out.println("search end");
return FileVisitResult.TERMINATE;
}
return FileVisitResult.CONTINUE;
}
}
package me.whiteship.designpatterns._03_behavioral_patterns._23_visitor._03_java;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class VisitorInJava {
public static void main(String[] args) throws IOException {
Path startingDirectory = Path.of("/Users/keesun/workspace/design-patterns");
SearchFileVisitor searchFileVisitor =
new SearchFileVisitor("Triangle.java", startingDirectory);
Files.walkFileTree(startingDirectory, searchFileVisitor);
}
}
01.2 스프링
- BeanDefinitionVisitor
-
package me.whiteship.designpatterns._03_behavioral_patterns._23_visitor._03_java; import org.springframework.beans.factory.config.BeanDefinitionVisitor; public class VisitorInSpring { public static void main(String[] args) { BeanDefinitionVisitor beanDefinitionVisitor; } }
- 거의 쓸일은 없음
728x90
반응형
'CS Study > 디자인패턴' 카테고리의 다른 글
22-04-12-플라이웨이트패턴-2부-패턴적용하기 (0) | 2022.04.12 |
---|---|
22-04-12-플라이웨이트패턴-1부-패턴소개 (0) | 2022.04.12 |
22-04-11-비지터패턴-3부-장점과단점 (0) | 2022.04.12 |
22-04-11-비지터패턴-2부-패턴적용하기 (0) | 2022.04.12 |
22-04-11-비지터패턴-1부-패턴소개 (0) | 2022.04.12 |
댓글