VisitorPattern

<textarea cols="50" rows="15" name="code" class="java">package VisitorPattern; public interface Acceptor { public abstract void accept(Visitor v); } package VisitorPattern; import java.util.Iterator; public abstract class Entry implements Acceptor { public abstract String getName(); public abstract int getSize(); public Entry add(Entry entry) throws FileTreatmentException{ throw new FileTreatmentException(); } public Iterator iterator() throws FileTreatmentException{ throw new FileTreatmentException(); } public String toString(){ return getName() + &quot;(&quot;+getSize()+&quot;)&quot;; } } package VisitorPattern; import java.util.Iterator; import java.util.Vector; public class Directory extends Entry{ private String name; private Vector dir = new Vector(); public Directory(String name){ this.name = name; } public String getName(){ return name; } public int getSize(){ int size = 0; Iterator it = dir.iterator(); while(it.hasNext()){ Entry entry = (Entry)it.next(); size += entry.getSize(); } return size; } public Entry add(Entry entry){ dir.add(entry); return this; } public Iterator iterator(){ return dir.iterator(); } public void accept(Visitor v){ v.visit(this); } } package VisitorPattern; public class File extends Entry{ private String name; private int size; public File(String name,int size){ this.name = name; this.size = size; } public int getSize(){ return size; } public String getName(){ return name; } public void accept(Visitor v){ v.visit(this); } } package VisitorPattern; public abstract class Visitor { public abstract void visit(File file); public abstract void visit(Directory directory); } package VisitorPattern; import java.util.Iterator; public class ListVisitor extends Visitor{ private String currentdir = &quot;&quot;; public void visit(File file){ System.out.println(currentdir + &quot;/&quot; + file); } public void visit(Directory directory){ System.out.println(currentdir + &quot;/&quot; + directory); String savedir = currentdir; currentdir = currentdir + &quot;/&quot; + directory.getName(); Iterator it = directory.iterator(); while(it.hasNext()){ Entry entry = (Entry)it.next(); entry.accept(this); } currentdir = savedir; } } package VisitorPattern; public class FileTreatmentException extends RuntimeException{ public FileTreatmentException(){} public FileTreatmentException(String msg){ super(msg); } } package VisitorPattern; public class Main { public static void main(String[] args){ try { System.out.println(&quot;Making root entries...&quot;); Directory rootdir = new Directory(&quot;root&quot;); Directory bindir = new Directory(&quot;bin&quot;); Directory tmpdir = new Directory(&quot;tmp&quot;); Directory usrdir = new Directory(&quot;usr&quot;); rootdir.add(bindir); rootdir.add(tmpdir); rootdir.add(usrdir); bindir.add(new File(&quot;vi&quot;,1000)); bindir.add(new File(&quot;latex&quot;,2000)); rootdir.accept(new ListVisitor()); System.out.println(&quot;&quot;); System.out.println(&quot;Making user entries...&quot;); Directory yuki = new Directory(&quot;yuki&quot;); Directory hanako = new Directory(&quot;hanako&quot;); Directory tomura = new Directory(&quot;tomura&quot;); usrdir.add(yuki); usrdir.add(hanako); usrdir.add(tomura); yuki.add(new File(&quot;diary.html&quot;,100)); yuki.add(new File(&quot;Composizt.java&quot;,200)); hanako.add(new File(&quot;memo.tex&quot;,300)); tomura.add(new File(&quot;game.doc&quot;,400)); rootdir.accept(new ListVisitor()); tomura.add(new File(&quot;junk.mail&quot;,500)); } catch (FileTreatmentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </textarea>

你可能感兴趣的:(VisitorPattern)