carfield.com.hk BeeAndFlowers.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c11:BeeAndFlowers.java // Demonstration of &quot;visitor&quot; pattern. import java.util.*; import com.bruceeckel.test.*; interface Visitor { void visit(Gladiolus g); void visit(Renuculus r); void visit(Chrysanthemum c); } // The Flower hierarchy cannot be changed: interface Flower { void accept(Visitor v); } class Gladiolus implements Flower { public void accept(Visitor v) { v.visit(this);} } class Renuculus implements Flower { public void accept(Visitor v) { v.visit(this);} } class Chrysanthemum implements Flower { public void accept(Visitor v) { v.visit(this);} } // Add the ability to produce a string: class StringVal implements Visitor { String s; public String toString() { return s; } public void visit(Gladiolus g) { s = &quot;Gladiolus&quot;; } public void visit(Renuculus r) { s = &quot;Renuculus&quot;; } public void visit(Chrysanthemum c) { s = &quot;Chrysanthemum&quot;; } } // Add the ability to do &quot;Bee&quot; activities: class Bee implements Visitor { public void visit(Gladiolus g) { System.out.println(&quot;Bee and Gladiolus&quot;); } public void visit(Renuculus r) { System.out.println(&quot;Bee and Renuculus&quot;); } public void visit(Chrysanthemum c) { System.out.println(&quot;Bee and Chrysanthemum&quot;); } } class FlowerGenerator { public static Flower newFlower() { switch((int)(Math.random() * 3)) { default: case 0: return new Gladiolus(); case 1: return new Renuculus(); case 2: return new Chrysanthemum(); } } } public class BeeAndFlowers extends UnitTest { List flowers = new ArrayList(); public BeeAndFlowers() { for(int i = 0; i &lt; 10; i++) flowers.add(FlowerGenerator.newFlower()); } public void test() { // It's almost as if I had a function to // produce a Flower string representation: StringVal sval = new StringVal(); Iterator it = flowers.iterator(); while(it.hasNext()) { ((Flower)it.next()).accept(sval); System.out.println(sval); } // Perform &quot;Bee&quot; operation on all Flowers: Bee bee = new Bee(); it = flowers.iterator(); while(it.hasNext()) ((Flower)it.next()).accept(bee); } public static void main(String args[]) { new BeeAndFlowers().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 PaperScissorsRock.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c11:PaperScissorsRock.java // Demonstration of multiple dispatching. import java.util.*; import com.bruceeckel.test.*; // An enumeration type: class Outcome { private int value; private String name; private Outcome(int val, String nm) { value = val; name = nm; } public final static Outcome WIN = new Outcome(0, &quot;win&quot;), LOSE = new Outcome(1, &quot;lose&quot;), DRAW = new Outcome(2, &quot;draw&quot;); public String toString() { return name; } public boolean equals(Object o) { return (o instanceof Outcome) &amp;&amp; (value == ((Outcome)o).value); } } interface Item { Outcome compete(Item it); Outcome eval(Paper p); Outcome eval(Scissors s); Outcome eval(Rock r); } class Paper implements Item { public Outcome compete(Item it) { return it.eval(this); } public Outcome eval(Paper p) { return Outcome.DRAW; } public Outcome eval(Scissors s) { return Outcome.WIN; } public Outcome eval(Rock r) { return Outcome.LOSE; } public String toString() { return &quot;Paper&quot;; } } class Scissors implements Item { public Outcome compete(Item it) { return it.eval(this); } public Outcome eval(Paper p) { return Outcome.LOSE; } public Outcome eval(Scissors s) { return Outcome.DRAW; } public Outcome eval(Rock r) { return Outcome.WIN; } public String toString() { return &quot;Scissors&quot;; } } class Rock implements Item { public Outcome compete(Item it) { return it.eval(this); } public Outcome eval(Paper p) { return Outcome.WIN; } public Outcome eval(Scissors s) { return Outcome.LOSE; } public Outcome eval(Rock r) { return Outcome.DRAW; } public String toString() { return &quot;Rock&quot;; } } class ItemGenerator { public static Item newItem() { switch((int)(Math.random() * 3)) { default: case 0: return new Scissors(); case 1: return new Paper(); case 2: return new Rock(); } } } class Compete { public static Outcome match(Item a, Item b) { System.out.print(a + &quot; &lt;--&gt; &quot; + b + &quot; : &quot;); return a.compete(b); } } public class PaperScissorsRock extends UnitTest { List items1 = new ArrayList(), items2 = new ArrayList(); static int SIZE = 20; public PaperScissorsRock() { for(int i = 0; i &lt; SIZE; i++) { items1.add(ItemGenerator.newItem()); items2.add(ItemGenerator.newItem()); } } public void test() { for(int i = 0; i &lt; SIZE; i++) System.out.println( Compete.match( (Item)items1.get(i), (Item)items2.get(i))); } public static void main(String args[]) { new PaperScissorsRock().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 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 .\c11 # 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: \ PaperScissorsRock.class \ BeeAndFlowers.class jikes: \ PaperScissorsRock.class \ BeeAndFlowers.class clean: ifeq ($(notdir $(SHELL)),COMMAND.COM) del *.class else rm -f *.class endif PaperScissorsRock.class: PaperScissorsRock.java $(JVC) $(JVCFLAGS) $&lt; java com.bruceeckel.test.RunUnitTests PaperScissorsRock BeeAndFlowers.class: BeeAndFlowers.java $(JVC) $(JVCFLAGS) $&lt; java com.bruceeckel.test.RunUnitTests BeeAndFlowers </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