import java.util.ArrayList; //Component Node, common interface interface AbstractFile { public void ls(); } // File implements the common interface, a Leaf class File implements AbstractFile { private String m_name; public File(String name) { m_name = name; } public void ls() { System.out.println(CompositeDemo.g_indent + m_name); } } // Directory implements the common interface, a composite class Directory implements AbstractFile { private String m_name; private ArrayList m_files = new ArrayList(); public Directory(String name) { m_name = name; } public void add(AbstractFile obj) { m_files.add(obj); } public void ls() { System.out.println(CompositeDemo.g_indent + m_name); CompositeDemo.g_indent.append(" "); // add 3 spaces for (int i = 0; i < m_files.size(); ++i) { AbstractFile obj = m_files.get(i); obj.ls(); } //remove the 3 spaces: CompositeDemo.g_indent.setLength(CompositeDemo.g_indent.length() - 3); } } public class CompositeDemo { public static StringBuffer g_indent = new StringBuffer(); public static void main(String[] args) { Directory one = new Directory("dir111"), two = new Directory("dir222"), thr = new Directory("dir333"); File a = new File("a"), b = new File("b"), c = new File("c"), d = new File("d"), e = new File("e"); one.add(a); one.add(two); one.add(b); two.add(c); two.add(d); two.add(thr); thr.add(e); one.ls(); } }