[Java] Composite pattern

2 minute read

What is the ### Composite pattern? The container and the contents are identified and a recursive structure is created.

Leaf role

A role that represents “contents”. Nothing else can fit in this role.

``’’ java 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); } }


### Composite role
A role that represents a "container".
You can put Leaf and Composite roles.

``'' java
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);
}
}
}

Component role

A role to identify the Leaf role and the Composite role. Component role is realized as a superclass common to Leaf role and Composite role.

``’’ java 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() + “)”; } }


### Client (requester) role
Users of the Composite pattern.

``'' java
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();
It's a sequel.
System.out.println();
It's a sequel.
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();
}
}
}

Execution result

![Screenshot 2020-09-09 11.58.42.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/197834/55e32b72-6e82-49ab-6d21-(0f7c5434a2ff.png)

https://github.com/aki0207/composite

I referred to this. Introduction to Design Patterns Learned from the Enhanced and Revised Java Language

Tags:

Updated: