繝繧カ繧、繝ウ繝代ち繝シ繝ウ Compositeシ壼ョケ蝎ィ縺ィ荳ュ霄ォ縺ョ蜷御ク隕

讎りヲ

Compositeシ晄キキ蜷育黄
繝繧」繝ャ繧ッ繝医Μ縺ィ繝輔ぃ繧、繝ォ縺ョ讒倥↑蜀榊クー逧縺ェ讒矩縺九i縺ェ繧九が繝悶ず繧ァ繧ッ繝医r逕滓舌☆繧九ヱ繧ソ繝シ繝ウ

繧ッ繝ゥ繧ケ蝗ウ


譛ャ萓

謚ス雎。

Leaf(闡)

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 this.name;
縲}

@Override
縲public int getSize() {
縲縲return this.size;
縲}

@Override
protected void printList(String prefix) {}
}

Composite(隍蜷井ス)

public class Directory extends Entry {
縲private String name;
縲private ArrayList<Entry> directory = new ArrayList<Entry>();

縲public Directory(String name) {
縲縲this.name = name;
縲}

縲public Entry add(Entry entry) {
縲縲this.directory.add(entry);
縲縲return this.toStrong();
縲}

@Override
縲public String getName() {
縲縲return this.name;
縲}

@Override
縲public int getSize() {
縲縲int size = 0;
縲縲繝繧」繝ャ繧ッ繝医Μ(繝繧」繝ャ繧ッ繝医Μ縲√ヵ繧。繧、繝ォ)繧貞ィ莉カLoop
縲縲Iterator<?> it = this.directory.iterator();
縲縲while (it.hasNext()) {
縲縲縲Entry entry = (Entry)it.next();
縲縲縲size += entry.getSize();
縲縲}
縲縲return size;
縲}

@Override
縲protected void printList(String prefix) {
縲縲System.out.println(prefix + "/" + this.toString());
縲縲繝繧」繝ャ繧ッ繝医Μ(繝繧」繝ャ繧ッ繝医Μor繝輔ぃ繧、繝ォ)繧貞ィ莉カLoop
縲縲Iterator<?> it = this.directory.iterator();
縲縲while (it.hasNext()) {
縲縲縲Entry entry = (Entry) it.next();
縲縲縲entry==繝輔ぃ繧、繝ォ譎ゑシ夊ヲェ繝繧」繝ャ繧ッ繝医Μ蜷阪→閾ェ霄ォ縺ョ蜷榊燕繧貞コ蜉
縲縲縲窶サFile繧ッ繝ゥ繧ケ縺ョprintList()蜻シ縺ウ蜃コ縺
縲縲縲entry==繝繧」繝ャ繧ッ繝医Μ譎ゑシ夊ヲェ繝繧」繝ャ繧ッ繝医Μ蜷阪→閾ェ霄ォ縺ョ蜷榊燕繧貞コ蜉
縲縲縲窶サ隕ェ繝繧」繝ャ繧ッ繝医Μ蜷阪→閾ェ霄ォ縺ョ蜷榊燕繧貞シ墓焚縺ォDirectory繧ッ繝ゥ繧ケ縺ョ譛ャ繝。繧ス繝繝峨r蜀榊クー逧縺ォ蜻シ縺ウ蜃コ縺

縲縲縲entry.printList(prefix + "/" + this.name);
縲縲}
縲}
}

Component

public class FileTreatmentException extends Exception {

縲public FileTreatmentException() {}

縲public FileTreatmentException(String msg) {
縲縲super(msg);
縲}
}


public abstract class Entry {
縲public Entry add(Entry entry) throws FileTreatmentException {
縲縲throw new FileTreatmentException();
縲}
蜈ャ髢九Γ繧ス繝繝
縲public void printList() {
縲縲髱槫ャ髢九Γ繧ス繝繝峨r蜻シ縺ウ蜃コ縺
縲縲this.printList("");
縲};
縲public String toString() {
縲縲return this.getName() + "(" + this.getSize() + ")";
縲}
縲public abstract String getName();
縲public abstract int getSize();
protected abstract void printList(String prefix);
}

Client(蛻ゥ逕ィ閠)

public class Main {

縲public static void main(String[] args) {
縲縲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("");
縲縲Directory java = new Directory("JAVA");
縲縲Directory php = new Directory("PHP");
縲縲Directory c = new Directory("C#");
縲縲usrdir.add(java);
縲縲usrdir.add(php);
縲縲usrdir.add(c);
縲縲java.add(new File("java1.html", 100));
縲縲java.add(new File("java2.java", 200));
縲縲php.add(new File("php1.html", 300));
縲縲php.add(new File("php2.php", 400));
縲縲c.add(new File("c1.c", 500));
縲縲c.add(new File("c2.c", 600));
縲縲rootdir.printList();
縲}
}

繝繧」繝ャ繧ッ繝医Μ讒区
root
笏巴in
縲笏牌i
縲笏罵atex
笏杯mp
笏盃sr
縲笏寧AVA
縲縲笏破ava1.html
縲縲笏破ava2.java
縲笏捻HP
縲縲笏廃hp1.html
縲縲笏廃hp2.php
縲笏任#
縲縲笏把1.c
縲縲笏把2.c


邨先棡
/root(30000)
/root/bin(30000)
/root/tmp(0)
/root/usr(0)

/root(32100)
/root/bin(30000)
/root/tmp(0)
/root/usr(2100)
/root/usr/JAVA(300)
/root/usr/PHP(700)
/root/usr/C#(1100)

Follow me!