SE450: Visitor Example [47/55] Previous pageContentsNext page

file:horstmann/ch10_visitor/FileSystemNode.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
package horstmann.ch10_visitor;
/**
   The common interface for file and directory nodes.
 */
public interface FileSystemNode
{
  void accept(FileSystemVisitor v);
}

file:horstmann/ch10_visitor/FileNode.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
package horstmann.ch10_visitor;
import java.io.File;

public class FileNode implements FileSystemNode
{
  public FileNode(File file)
  {
    this.file = file;
  }

  public File getFile() { return file; }

  public void accept(FileSystemVisitor v)
  {
    v.visitFileNode(this);
  }

  private File file;
}

file:horstmann/ch10_visitor/DirectoryNode.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package horstmann.ch10_visitor;
import java.io.File;

public class DirectoryNode implements FileSystemNode
{
  public DirectoryNode(File directory)
  {
    this.directory = directory;
  }

  public void accept(FileSystemVisitor v)
  {
    v.visitDirectoryNode(this);
  }

  public File getDirectory() { return directory; }

  public FileSystemNode[] getChildren()
  {
    File[] files = directory.listFiles();
    FileSystemNode[] children = new FileSystemNode[files.length];
    for (int i = 0; i < files.length; i++)
    {
      File f = files[i];
      if (f.isDirectory())
        children[i] = new DirectoryNode(f);
      else
        children[i] = new FileNode(f);
    }
    return children;
  }

  private File directory;
}

file:horstmann/ch10_visitor/FileSystemVisitor.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
package horstmann.ch10_visitor;
/**
   The visitor interface type for visiting file system nodes.
 */
public interface FileSystemVisitor
{
  /**
      Visits a file node.
      @param node the file node
   */
  void visitFileNode(FileNode node);

  /**
      Visits a directory node.
      @param node the directory node
   */
  void visitDirectoryNode(DirectoryNode node);
}

file:horstmann/ch10_visitor/PrintVisitor.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
package horstmann.ch10_visitor;

public class PrintVisitor implements FileSystemVisitor
{
  public void visitFileNode(FileNode node)
  {
    for (int i = 0; i < level; i++) System.out.print(" ");
    System.out.println(node.getFile().getName());
  }

  public void visitDirectoryNode(DirectoryNode node)
  {
    for (int i = 0; i < level; i++) System.out.print(" ");
    System.out.println(node.getDirectory().getName());
    level++;
    for (FileSystemNode c : node.getChildren())
      c.accept(this);
    level--;
  }

  private int level = 0;
}

file:horstmann/ch10_visitor/VisitorTester.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
package horstmann.ch10_visitor;
import java.io.File;

public class VisitorTester
{
  public static void main(String[] args)
  {
    DirectoryNode node = new DirectoryNode(new File(".."));
    node.accept(new PrintVisitor());
  }
}

Previous pageContentsNext page