Interface
Problem
사실 나는 interface에 대해 이렇게 이해했었다.
Solution
// FilenameFilter는 interface이고 accept메서드 하나를 가진다.
public void clientCode(){
FileDialog dialog = new FileDialog(parent, "select - ", FileDialog.LOAD);
//Interface.
FilenameFilter filter = new FilenameFilter(){
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".xml");
}
};
//client Code
dialog.setFilenameFilter(filter);
dialog.setVisible(true);
String selectedFile = dialog.getFile();
}
예제와 설명은 봤는데 사실 잘 와닿지가 않는다 음..!
interface를 활용함으로써 필터링할 로직을 client가 결정할 수 있게끔 해주는것인가?
예제를 살펴보면 client 코드에서 filter를 생성하는데, filter는 interface이므로 반드시 accept 메서드를 구현해야 한다.
그리고 accept메서는 .xml로 끝나는 파일만 반환한다. 아마 최종적으로는 xml리스트만 남겠지.
'Today I learned' 카테고리의 다른 글
Java Synchronization (0) | 2021.08.06 |
---|---|
재사용 가능한 디자인 패턴 기반 코딩기술 강의 정리 - Abstract superclass (0) | 2021.08.05 |
재사용 가능한 디자인 패턴 기반 코딩기술 강의 정리 - Delegation (0) | 2021.08.02 |
재사용 가능한 디자인 패턴 기반 코딩기술 강의 (2) (0) | 2021.07.31 |
언니가 친절하게 알려주는 문제를 해결하는 흐름.(with js로 fadeIn 구현) (2) | 2021.07.09 |
댓글