carfield.com.hk BoxObserver.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c10:BoxObserver.java // Demonstration of Observer pattern using // Java's built-in observer classes. import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import com.bruceeckel.swing.*; // You must inherit a new type of Observable: class BoxObservable extends Observable { public void notifyObservers(Object b) { // Otherwise it won't propagate changes: setChanged(); super.notifyObservers(b); } } public class BoxObserver extends JFrame { Observable notifier = new BoxObservable(); public BoxObserver(int grid) { setTitle(&quot;Demonstrates Observer pattern&quot;); Container cp = getContentPane(); cp.setLayout(new GridLayout(grid, grid)); for(int x = 0; x &lt; grid; x++) for(int y = 0; y &lt; grid; y++) cp.add(new OCBox(x, y, notifier)); } public static void main(String[] args) { int grid = 8; if(args.length &gt; 0) grid = Integer.parseInt(args[0]); JFrame f = new BoxObserver(grid); f.setSize(500, 400); f.setVisible(true); // JDK 1.3: f.setDefaultCloseOperation(EXIT_ON_CLOSE); // Add a WindowAdapter if you have JDK 1.2 } } class OCBox extends JPanel implements Observer { Observable notifier; int x, y; // Locations in grid Color cColor = newColor(); static final Color[] colors = { Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow }; static final Color newColor() { return colors[ (int)(Math.random() * colors.length) ]; } OCBox(int x, int y, Observable notifier) { this.x = x; this.y = y; notifier.addObserver(this); this.notifier = notifier; addMouseListener(new ML()); } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(cColor); Dimension s = getSize(); g.fillRect(0, 0, s.width, s.height); } class ML extends MouseAdapter { public void mousePressed(MouseEvent e) { notifier.notifyObservers(OCBox.this); } } public void update(Observable o, Object arg) { OCBox clicked = (OCBox)arg; if(nextTo(clicked)) { cColor = clicked.cColor; repaint(); } } private final boolean nextTo(OCBox b) { return Math.abs(x - b.x) &lt;= 1 &amp;&amp; Math.abs(y - b.y) &lt;= 1; } } ///:~ </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 ObservedFlower.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c10:ObservedFlower.java // Demonstration of &quot;observer&quot; pattern. import java.util.*; import com.bruceeckel.test.*; class Flower { private boolean isOpen; private OpenNotifier oNotify = new OpenNotifier(); private CloseNotifier cNotify = new CloseNotifier(); public Flower() { isOpen = false; } public void open() { // Opens its petals isOpen = true; oNotify.notifyObservers(); cNotify.open(); } public void close() { // Closes its petals isOpen = false; cNotify.notifyObservers(); oNotify.close(); } public Observable opening() { return oNotify; } public Observable closing() { return cNotify; } private class OpenNotifier extends Observable { private boolean alreadyOpen = false; public void notifyObservers() { if(isOpen &amp;&amp; !alreadyOpen) { setChanged(); super.notifyObservers(); alreadyOpen = true; } } public void close() { alreadyOpen = false; } } private class CloseNotifier extends Observable{ private boolean alreadyClosed = false; public void notifyObservers() { if(!isOpen &amp;&amp; !alreadyClosed) { setChanged(); super.notifyObservers(); alreadyClosed = true; } } public void open() { alreadyClosed = false; } } } class Bee { private String name; private OpenObserver openObsrv = new OpenObserver(); private CloseObserver closeObsrv = new CloseObserver(); public Bee(String nm) { name = nm; } // An inner class for observing openings: private class OpenObserver implements Observer{ public void update(Observable ob, Object a) { System.out.println(&quot;Bee &quot; + name + &quot;'s breakfast time!&quot;); } } // Another inner class for closings: private class CloseObserver implements Observer{ public void update(Observable ob, Object a) { System.out.println(&quot;Bee &quot; + name + &quot;'s bed time!&quot;); } } public Observer openObserver() { return openObsrv; } public Observer closeObserver() { return closeObsrv; } } class Hummingbird { private String name; private OpenObserver openObsrv = new OpenObserver(); private CloseObserver closeObsrv = new CloseObserver(); public Hummingbird(String nm) { name = nm; } private class OpenObserver implements Observer{ public void update(Observable ob, Object a) { System.out.println(&quot;Hummingbird &quot; + name + &quot;'s breakfast time!&quot;); } } private class CloseObserver implements Observer{ public void update(Observable ob, Object a) { System.out.println(&quot;Hummingbird &quot; + name + &quot;'s bed time!&quot;); } } public Observer openObserver() { return openObsrv; } public Observer closeObserver() { return closeObsrv; } } public class ObservedFlower extends UnitTest { Flower f = new Flower(); Bee ba = new Bee(&quot;A&quot;), bb = new Bee(&quot;B&quot;); Hummingbird ha = new Hummingbird(&quot;A&quot;), hb = new Hummingbird(&quot;B&quot;); public void test() { f.opening().addObserver(ha.openObserver()); f.opening().addObserver(hb.openObserver()); f.opening().addObserver(ba.openObserver()); f.opening().addObserver(bb.openObserver()); f.closing().addObserver(ha.closeObserver()); f.closing().addObserver(hb.closeObserver()); f.closing().addObserver(ba.closeObserver()); f.closing().addObserver(bb.closeObserver()); // Hummingbird B decides to sleep in: f.opening().deleteObserver( hb.openObserver()); // A change that interests observers: f.open(); f.open(); // It's already open, no change. // Bee A doesn't want to go to bed: f.closing().deleteObserver( ba.closeObserver()); f.close(); f.close(); // It's already closed; no change f.opening().deleteObservers(); f.open(); f.close(); } public static void main(String args[]) { new ObservedFlower().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 .\c10 # 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: \ ObservedFlower.class \ BoxObserver.class jikes: \ ObservedFlower.class \ BoxObserver.class clean: ifeq ($(notdir $(SHELL)),COMMAND.COM) del *.class else rm -f *.class endif ObservedFlower.class: ObservedFlower.java $(JVC) $(JVCFLAGS) $&lt; java com.bruceeckel.test.RunUnitTests ObservedFlower BoxObserver.class: BoxObserver.java </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