carfield.com.hk Games.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c05:Games.java // An example of the Abstract Factory pattern. import com.bruceeckel.test.*; interface Obstacle { void action(); } interface Player { void interactWith(Obstacle o); } class Kitty implements Player { public void interactWith(Obstacle ob) { System.out.print(&quot;Kitty has encountered a &quot;); ob.action(); } } class KungFuGuy implements Player { public void interactWith(Obstacle ob) { System.out.print(&quot;KungFuGuy now battles a &quot;); ob.action(); } } class Puzzle implements Obstacle { public void action() { System.out.println(&quot;Puzzle&quot;); } } class NastyWeapon implements Obstacle { public void action() { System.out.println(&quot;NastyWeapon&quot;); } } // The Abstract Factory: interface GameElementFactory { Player makePlayer(); Obstacle makeObstacle(); } // Concrete factories: class KittiesAndPuzzles implements GameElementFactory { public Player makePlayer() { return new Kitty(); } public Obstacle makeObstacle() { return new Puzzle(); } } class KillAndDismember implements GameElementFactory { public Player makePlayer() { return new KungFuGuy(); } public Obstacle makeObstacle() { return new NastyWeapon(); } } class GameEnvironment { private GameElementFactory gef; private Player p; private Obstacle ob; public GameEnvironment( GameElementFactory factory) { gef = factory; p = factory.makePlayer(); ob = factory.makeObstacle(); } public void play() { p.interactWith(ob); } } public class Games extends UnitTest { GameElementFactory kp = new KittiesAndPuzzles(), kd = new KillAndDismember(); GameEnvironment g1 = new GameEnvironment(kp), g2 = new GameEnvironment(kd); // These just ensure no exceptions are thrown: public void test1() { g1.play(); } public void test2() { g2.play(); } public static void main(String args[]) { Games g = new Games(); g.test1(); g.test2(); } } ///:~ </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 ShapeFactory1.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c05:shapefact1:ShapeFactory1.java // A simple static factory method. package c05.shapefact1; import java.util.*; import com.bruceeckel.test.*; abstract class Shape { public abstract void draw(); public abstract void erase(); public static Shape factory(String type) { if(type.equals(&quot;Circle&quot;)) return new Circle(); if(type.equals(&quot;Square&quot;)) return new Square(); throw new RuntimeException( &quot;Bad shape creation: &quot; + type); } } class Circle extends Shape { Circle() {} // Friendly constructor public void draw() { System.out.println(&quot;Circle.draw&quot;); } public void erase() { System.out.println(&quot;Circle.erase&quot;); } } class Square extends Shape { Square() {} // Friendly constructor public void draw() { System.out.println(&quot;Square.draw&quot;); } public void erase() { System.out.println(&quot;Square.erase&quot;); } } public class ShapeFactory1 extends UnitTest { String shlist[] = { &quot;Circle&quot;, &quot;Square&quot;, &quot;Square&quot;, &quot;Circle&quot;, &quot;Circle&quot;, &quot;Square&quot; }; List shapes = new ArrayList(); public void test() { for(int i = 0; i &lt; shlist.length; i++) shapes.add(Shape.factory(shlist[i])); Iterator i = shapes.iterator(); while(i.hasNext()) { Shape s = (Shape)i.next(); s.draw(); s.erase(); } } public static void main(String args[]) { new ShapeFactory1().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 ShapeFactory2.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c05:shapefact2:ShapeFactory2.java // Polymorphic factory methods. package c05.shapefact2; import java.util.*; import com.bruceeckel.test.*; interface Shape { void draw(); void erase(); } abstract class ShapeFactory { protected abstract Shape create(); private static Map factories = new HashMap(); public static void addFactory(String id, ShapeFactory f) { factories.put(id, f); } // A Template Method: public static final Shape createShape(String id) { if(!factories.containsKey(id)) { try { // Load dynamically Class.forName(&quot;c05.shapefact2.&quot; + id); } catch(ClassNotFoundException e) { throw new RuntimeException( &quot;Bad shape creation: &quot; + id); } // See if it was put in: if(!factories.containsKey(id)) throw new RuntimeException( &quot;Bad shape creation: &quot; + id); } return ((ShapeFactory)factories.get(id)).create(); } } class Circle implements Shape { private Circle() {} public void draw() { System.out.println(&quot;Circle.draw&quot;); } public void erase() { System.out.println(&quot;Circle.erase&quot;); } private static class Factory extends ShapeFactory { protected Shape create() { return new Circle(); } } static { ShapeFactory.addFactory( &quot;Circle&quot;, new Factory()); } } class Square implements Shape { private Square() {} public void draw() { System.out.println(&quot;Square.draw&quot;); } public void erase() { System.out.println(&quot;Square.erase&quot;); } private static class Factory extends ShapeFactory { protected Shape create() { return new Square(); } } static { ShapeFactory.addFactory( &quot;Square&quot;, new Factory()); } } public class ShapeFactory2 extends UnitTest { String shlist[] = { &quot;Circle&quot;, &quot;Square&quot;, &quot;Square&quot;, &quot;Circle&quot;, &quot;Circle&quot;, &quot;Square&quot; }; List shapes = new ArrayList(); public void test() { // This just makes sure it will complete // without throwing an exception. for(int i = 0; i &lt; shlist.length; i++) shapes.add( ShapeFactory.createShape(shlist[i])); Iterator i = shapes.iterator(); while(i.hasNext()) { Shape s = (Shape)i.next(); s.draw(); s.erase(); } } public static void main(String args[]) { new ShapeFactory2().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 .\c05\shapefact2 # 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: \ ShapeFactory2.class jikes: \ ShapeFactory2.class clean: ifeq ($(notdir $(SHELL)),COMMAND.COM) del *.class else rm -f *.class endif ShapeFactory2.class: ShapeFactory2.java $(JVC) $(JVCFLAGS) $&lt; java com.bruceeckel.test.RunUnitTests c05.shapefact2.ShapeFactory2 </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