Composite pattern that is a combination mode. It can make the container and the contents have consistency, to create a recursive structure .
For example: in the file system, the contents of both folders, but also a container, having a consistency Thus, the file system form a recursive structure. The combination mode is used to create such a structure.
Code
The following code to the file system as an example to achieve a combined mode.
UML
Entry class
It represents a directory entry, the extracted common attributes of files and folders.
public abstract class Entry {
public abstract String getName();
public abstract int getSize();
public Entry add(Entry entry) throws RuntimeException {
throw new RuntimeException();
}
public void printList() {
printList("");
}
protected abstract void printList(String prefix);
public String toString() {
return getName() + " (size : " + getSize() + ")";
}
}
File type
It represents a file
public class File extends Entry {
private String name;
private int size;
public File(String name, int size) {
this.name = name;
this.size = size;
}
public String getName() {
return name;
}
public int getSize() {
return size;
}
protected void printList(String prefix) {
System.out.println(prefix + "/" + this);
}
}
Directory class
Indicates a folder
public class Directory extends Entry {
private String name;
private ArrayList directory = new ArrayList();
public Directory(String name) {
this.name = name;
}
public String getName() {
return name;
}
//获取文件夹的总大小,递归操作
public int getSize() {
int size = 0;
Iterator 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;
}
protected void printList(String prefix) {
System.out.println(prefix + "/" + this);
Iterator it = directory.iterator();
while (it.hasNext()) {
Entry entry = (Entry)it.next();
entry.printList(prefix + "/" + name);
}
}
}
Main category
public class Main {
public static void main(String[] args) {
try {
System.out.println("创建根目录");
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("创建用户目录");
Directory xiaoming = new Directory("小明");
Directory xiaohong = new Directory("小红");
Directory xiaohua = new Directory("小花");
usrdir.add(xiaoming);
usrdir.add(xiaohong);
usrdir.add(xiaohua);
xiaoming.add(new File("diary.html", 100));
xiaoming.add(new File("Composite.java", 200));
xiaohua.add(new File("memo.tex", 300));
xiaohong.add(new File("game.doc", 400));
xiaohong.add(new File("junk.mail", 500));
rootdir.printList();
} catch (FileTreatmentException e) {
e.printStackTrace();
}
}
}
结果
/*
创建根目录
/root (size : 30000)
/root/bin (size : 30000)
/root/bin/vi (size : 10000)
/root/bin/latex (size : 20000)
/root/tmp (size : 0)
/root/usr (size : 0)
创建用户目录
/root (size : 31500)
/root/bin (size : 30000)
/root/bin/vi (size : 10000)
/root/bin/latex (size : 20000)
/root/tmp (size : 0)
/root/usr (size : 1500)
/root/usr/小明 (size : 300)
/root/usr/小明/diary.html (size : 100)
/root/usr/小明/Composite.java (size : 200)
/root/usr/小红 (size : 900)
/root/usr/小红/game.doc (size : 400)
/root/usr/小红/junk.mail (size : 500)
/root/usr/小花 (size : 300)
/root/usr/小花/memo.tex (size : 300)
*/
Character
The Component : Leaf and Composite is the role of the parent, contain consistent information which has two roles. In this case, the Entry to play this role.
Leaf : represents the role of content, which can not be put into other objects. In this case by the File to play this role.
Composite : represents the role of the container, which can be placed Leaf or Composite roles. In this case by the Directory to play this role.
Client : Use Composite role model, in this case played by the Main.
UML
idea
This mode is very embody the idea of recursion. Composite inherit and a combination of the Component , good taste.