carfield.com.hk FillableVisitor.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c12:trashvisitor:FillableVisitor.java // Adapter Decorator that adds the visitable // decorator as the Trash objects are // being created. import c12.trash.*; import java.util.*; public class FillableVisitor implements Fillable { private Fillable f; public FillableVisitor(Fillable ff) { f = ff; } public void addTrash(Trash t) { f.addTrash(new VisitableDecorator(t)); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z TrashVisitor.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c12:trashvisitor:TrashVisitor.java // The &quot;visitor&quot; pattern with VisitableDecorators. import c12.trash.*; import java.util.*; import com.bruceeckel.test.*; // Specific group of algorithms packaged // in each implementation of Visitor: class PriceVisitor implements Visitor { private double alSum; // Aluminum private double pSum; // Paper private double gSum; // Glass private double cSum; // Cardboard public void visit(Aluminum al) { double v = al.getWeight() * al.getValue(); System.out.println( &quot;value of Aluminum= &quot; + v); alSum += v; } public void visit(Paper p) { double v = p.getWeight() * p.getValue(); System.out.println( &quot;value of Paper= &quot; + v); pSum += v; } public void visit(Glass g) { double v = g.getWeight() * g.getValue(); System.out.println( &quot;value of Glass= &quot; + v); gSum += v; } public void visit(Cardboard c) { double v = c.getWeight() * c.getValue(); System.out.println( &quot;value of Cardboard = &quot; + v); cSum += v; } void total() { System.out.println( &quot;Total Aluminum: $&quot; + alSum + &quot;\n Total Paper: $&quot; + pSum + &quot;\nTotal Glass: $&quot; + gSum + &quot;\nTotal Cardboard: $&quot; + cSum + &quot;\nTotal: $&quot; + (alSum + pSum + gSum + cSum)); } } class WeightVisitor implements Visitor { private double alSum; // Aluminum private double pSum; // Paper private double gSum; // Glass private double cSum; // Cardboard public void visit(Aluminum al) { alSum += al.getWeight(); System.out.println(&quot;weight of Aluminum = &quot; + al.getWeight()); } public void visit(Paper p) { pSum += p.getWeight(); System.out.println(&quot;weight of Paper = &quot; + p.getWeight()); } public void visit(Glass g) { gSum += g.getWeight(); System.out.println(&quot;weight of Glass = &quot; + g.getWeight()); } public void visit(Cardboard c) { cSum += c.getWeight(); System.out.println(&quot;weight of Cardboard = &quot; + c.getWeight()); } void total() { System.out.println( &quot;Total weight Aluminum: &quot; + alSum + &quot;\nTotal weight Paper: &quot; + pSum + &quot;\nTotal weight Glass: &quot; + gSum + &quot;\nTotal weight Cardboard: &quot; + cSum + &quot;\nTotal weight: &quot; + (alSum + pSum + gSum + cSum)); } } public class TrashVisitor extends UnitTest { Collection bin = new ArrayList(); PriceVisitor pv = new PriceVisitor(); WeightVisitor wv = new WeightVisitor(); public TrashVisitor() { ParseTrash.fillBin(&quot;../trash/Trash.dat&quot;, new FillableVisitor( new FillableCollection(bin))); } public void test() { Iterator it = bin.iterator(); while(it.hasNext()) { Visitable v = (Visitable)it.next(); v.accept(pv); v.accept(wv); } pv.total(); wv.total(); } public static void main(String args[]) { new TrashVisitor().test(); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z VAluminum.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c12:trashvisitor:VAluminum.java // Taking the previous approach of creating a // specialized Aluminum for the visitor pattern. import c12.trash.*; public class VAluminum extends Aluminum implements Visitable { public VAluminum(double wt) { super(wt); } public void accept(Visitor v) { v.visit(this); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z Visitable.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c12:trashvisitor:Visitable.java // An interface to add visitor functionality // to the Trash hierarchy without // modifying the base class. import c12.trash.*; interface Visitable { // The new method: void accept(Visitor v); } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z VisitableDecorator.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c12:trashvisitor:VisitableDecorator.java // A decorator that adapts the generic Trash // classes to the visitor pattern. import c12.trash.*; import java.lang.reflect.*; public class VisitableDecorator extends Trash implements Visitable { private Trash delegate; private Method dispatch; public VisitableDecorator(Trash t) { delegate = t; try { dispatch = Visitor.class.getMethod ( &quot;visit&quot;, new Class[] { t.getClass() } ); } catch (Exception ex) { ex.printStackTrace(); } } public double getValue() { return delegate.getValue(); } public double getWeight() { return delegate.getWeight(); } public void accept(Visitor v) { try { dispatch.invoke(v, new Object[]{delegate}); } catch (Exception ex) { ex.printStackTrace(); } } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z Visitor.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c12:trashvisitor:Visitor.java // The base interface for visitors. import c12.trash.*; interface Visitor { void visit(Aluminum a); void visit(Paper p); void visit(Glass g); void visit(Cardboard c); } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z makefile 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="" rows="16" cols="100"># From Thinking in Patterns (with Java) by Bruce Eckel # At http://www.BruceEckel.com # (c)2001 Bruce Eckel # Copyright notice in Copyright.txt # Automatically-generated MAKEFILE # For examples in directory .\c12\trashvisitor # using the JDK 1.3 compiler # Invoke with: make HOME := ../../ ifndef MAKECMDGOALS MAKECMDGOALS := javac endif # Command.com is too weak to build this under Windows NT/2000: ifeq ($(OS),Windows_NT) COMSPEC=$(SYSTEMROOT)\system32\cmd.exe endif ifneq ($(MAKECMDGOALS),clean) include $(HOME)/$(MAKECMDGOALS).mac endif .SUFFIXES : .class .java .java.class : $(JVC) $(JVCFLAGS) $&lt; javac: \ Visitable.class \ Visitor.class \ VAluminum.class \ VisitableDecorator.class \ FillableVisitor.class \ TrashVisitor.class jikes: \ Visitable.class \ Visitor.class \ VAluminum.class \ VisitableDecorator.class \ FillableVisitor.class \ TrashVisitor.class clean: ifeq ($(notdir $(SHELL)),COMMAND.COM) del *.class else rm -f *.class endif Visitable.class: Visitable.java Visitor.class: Visitor.java VAluminum.class: VAluminum.java VisitableDecorator.class: VisitableDecorator.java FillableVisitor.class: FillableVisitor.java TrashVisitor.class: TrashVisitor.java $(JVC) $(JVCFLAGS) $&lt; java com.bruceeckel.test.RunUnitTests TrashVisitor </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z