Identify the container and contents to create a recursive structure.
A role that represents the "contents". Nothing else can be included in this role.
package composite;
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);
}
}
A role that represents a "container". You can enter the role of Leaf or Composite.
package composite;
import java.util.ArrayList;
import java.util.Iterator;
public class Directory extends Entry{
private String name;
private ArrayList<Entry> directory = new ArrayList<>();
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 = (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 = (Entry) it.next();
entry.printList(prefix + "/" + name);
}
}
}
A role to equate the Leaf role with the Composite role. The Component role is realized as a superclass common to the Leaf role and Composite role.
package composite;
public abstract class Entry {
public abstract String getName();
public abstract int getSize();
protected abstract void printList(String prefix);
public Entry add(Entry entry) throws FileTreatmentException {
throw new FileTreatmentException();
}
public void printList() {
printList("");
}
@Override
public String toString() {
return getName() + "(" + getSize() + ")";
}
}
Users of the Composite pattern.
package composite;
public class Main {
public static void main(String[] args) {
try {
System.out.println("Making root entryies...");
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 entryies...");
Directory yuki = new Directory("yuki");
Directory hanako = new Directory("hanako");
Directory tomura = new Directory("tomura");
usrDir.add(yuki);
usrDir.add(hanako);
usrDir.add(tomura);
yuki.add(new File("diary.html", 100));
yuki.add(new File("Composite.java", 200));
hanako.add(new File("memo.tex", 300));
tomura.add(new File("game.doc", 400));
tomura.add(new File("junk.mail", 500));
rootDir.printList();
} catch (FileTreatmentException e) {
e.printStackTrace();
}
}
}
https://github.com/aki0207/composite
I used this as a reference. Augmented and Revised Introduction to Design Patterns Learned in Java Language
Recommended Posts