디렉토리(directory)
통산 윈도우에서는 폴더라고 부르는 이것은 '상자 안의 상자', 즉 디렉토리 안에 디렉토리나 파일을 가지고 있는 재귀적인 형태의 구조를 띄고 있다.
디렉토리 안에 넣을 수 있는 것, 디렉토리와 파일을 아울러 '디렉토리 엔트리(directory entry)'라고 한다.
디렉토리 안의 내용을 조사한다할때, 그것이 하위디렉토리인지, 파일인지 모르지만 통상 디렉토리 엔트리를 조사한다고 말할 수 있다.
이는 마치 그릇과 내용물의 같은 취급으로 표현할 수 있다.
마치 그릇 안에 내용물을 넣을 수 있고 더 작은 그릇을 넣을 수 있듯이...
이와 같이 그릇과 내용물을 동일시 해서 재귀적인 구조를 만들기 위한 디자인패턴을 "composite 패턴"이라고 한다.
그럼이제 파일과 디렉토리를 도식적으로 표현한 프로그램을 작성해보자.
파일을 나타내는 클래스 : class File
디렉토리를 나타내는 클래스 : class Directory
디렉토리를 나타내는 클래스 : abstract class Entry
package compose;
public abstract class Entry {
public abstract String getName();
public abstract int getSize();
public Entry add(Entry entry) throws FileTreatmentException {
throw new FileTreatmentException();
}
public void printList() {
printList("");
}
protected abstract void printList(String prefix);
@Override
public String toString() {
return getName() + " (" + getSize() + ")";
}
}
package compose;
public class FileTreatmentException extends RuntimeException {
public FileTreatmentException() {
}
public FileTreatmentException(String msg) {
super(msg);
}
}
package compose;
public class File extends Entry {
private String name;
private int size;
public File(String name, int size) {
this.name = name;
this.size = size;
}
@Override
public String getName() {
return name;
}
@Override
public int getSize() {
return size;
}
@Override
protected void printList(String prefix) {
System.out.println(prefix + "/" + this);
}
}
package compose;
import java.util.ArrayList;
import java.util.Iterator;
public class Directory extends Entry {
private String name;
private ArrayList<Entry> directory = new ArrayList<Entry>();
public Directory(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public int getSize() {
int size = 0;
Iterator<Entry> it = directory.iterator();
while(it.hasNext()) {
Entry entry = it.next();
size += entry.getSize();
}
return size;
}
public Entry add(Entry entry) {
directory.add(entry);
return this;
}
@Override
protected void printList(String prefix) {
System.out.println(prefix + "/" + this);
Iterator<Entry> it = directory.iterator();
while(it.hasNext()) {
Entry entry = it.next();
entry.printList(prefix + "/" + name);
}
}
}
package compose;
public class Main {
public static void main(String[] args) {
try {
System.out.println("Making root entries...");
Directory rootDir = new Directory("root");
Directory binDir = new Directory("bin");
Directory tmpDir = new Directory("tmp");
Directory usrDir = new Directory("usr");
rootDir.add(binDir);
rootDir.add(tmpDir);
rootDir.add(usrDir);
binDir.add(new File("vi", 10000));
binDir.add(new File("latex", 20000));
rootDir.printList();
System.out.println("");
System.out.println("Making user entries...");
Directory kim = new Directory("KIM");
Directory lee = new Directory("LEE");
Directory park = new Directory("PARK");
usrDir.add(kim);
usrDir.add(lee);
usrDir.add(park);
kim.add(new File("diary.html", 100));
kim.add(new File("Composite.java", 200));
lee.add(new File("memo.text", 300));
park.add(new File("game.doc", 400));
park.add(new File("junk.mail", 500));
rootDir.printList();
}catch(FileTreatmentException e) {
e.printStackTrace();
}
}
}
결과:
Making root entries...
/root (30000)
/root/bin (30000)
/root/bin/vi (10000)
/root/bin/latex (20000)
/root/tmp (0)
/root/usr (0)
Making user entries...
/root (31500)
/root/bin (30000)
/root/bin/vi (10000)
/root/bin/latex (20000)
/root/tmp (0)
/root/usr (1500)
/root/usr/KIM (300)
/root/usr/KIM/diary.html (100)
/root/usr/KIM/Composite.java (200)
/root/usr/LEE (300)
/root/usr/LEE/memo.text (300)
/root/usr/PARK (900)
/root/usr/PARK/game.doc (400)
/root/usr/PARK/junk.mail (500)
'자바' 카테고리의 다른 글
[자바] Abstract Factory 패턴 (0) | 2021.06.12 |
---|---|
[자바] bridge 패턴 (0) | 2021.06.07 |
[자바] Observer 패턴 (0) | 2021.06.03 |
[자바] 싱글톤이 보장받지 않는 상황 (0) | 2021.06.03 |
[자바] builder 패턴 (0) | 2021.06.03 |